Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help with Linked List in ASM

Status
Not open for further replies.

nitsakh

Member level 1
Joined
Nov 24, 2010
Messages
32
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,514
I want to write a simple linked list program with ASM .
Here's the code which I have tried to write(presently only for single digit numbers) ! However,it works only for 4 numbers and that too has sometimes some errors. I am new to assembly language.
So please help me as to what I am supposed to do,as I am in fix !


Code:
.model small

.data
	m1 db 10,13,'Enter Data :: $'
    m2 db 10,13,'Continue ?? $'
    head dw 0				;variable for head node address
.code
	mov ax,@data
	mov ds,ax

    mov bx,1				;size in paragraphs to allocate
    mov ah,48h				;function to allocate memory dynamically
    int 21h

    mov head,ax				;copy address of new block to head	

    mov dx,offset m1
    mov ah,09h				;Display string m1
    int 21h
    mov ah,01h				;Accept character from keyboard
    int 21h
    xor ah,ah
    mov bx,head
    mov [bx],ax				;copy accepted value to memory
    mov di,head				;copy address of head to di

    mov dx,offset m2
    mov ah,09h				;display string m2
    int 21h
    mov ah,01h				;Accept char
    int 21h
    cmp al,'y'				;Continue in case of Y
    je loop1				;else skip
    cmp al,'Y'
    je loop1
    jmp below
       
loop1:
    mov bx,1
    mov ah,48h				;Allocate 1 paragraph dynamically
    int 21h

    xor si,si
    mov [di+2],ax			;Store address of new paragraph in di+2
    xor di,di				;i.e.next pointer of previous node
    mov si,ax				;copy new address to si
    mov dx,offset m1
    mov ah,09h				;Display string m1
    int 21h
    mov ah,01h
    int 21h
    xor ah,ah
    mov word ptr [si],ax	;copy accepted value to memory
    mov di,si				;copy new address to di
    
    mov dx,offset m2
    mov ah,09h
    int 21h
    mov ah,01h
    int 21h
    cmp al,'y'
    je loop1
    cmp al,'Y'
    je loop1

below:
    mov word ptr [di+2],0	;set next field of last node to 0
    
disp:
    mov ah,02h				;Display character
    mov dx,10				
    int 21h
    mov dx,13
    int 21h
    mov bx,head				;copy address of 1st node to bx
    mov ah,02h
    mov dx,[bx]				;Display value at head node
    int 21h

lp:
    mov ah,02h
    add bx,2				;go to next field of current node
    mov dx,word ptr [bx]	;retrieve value at this address
    int 21h
    cmp word ptr [bx+2],0	;compare this value with zero
    jnz lp					;if it is zero,then stop displaying

exit:
    mov ah,4ch				;Exit program
    int 21h

end
 
Last edited:

First rule of programming - add comments!!

Although your code is short, without even an explanation it is written for DOS or a description of what the interrupts are supposed to do, it makes no sense to the vast majority of people.

Despite that, I can't see how your linked list can possibly work four times when you only have two bytes to store anything ! For a linked (or doubly linked) list to work it needs a data field and a link field. The data field holds your values and the link field holds the address of the next item.

Brian.
 
Thanks ! I'll add comments !and the function mov ah,48h allocates one paragraph of memory. So every time in the loop I allocate 16 bytes,for the node.
Then in the first 2 bytes I store data,and address in the next two bytes.

My program is working for 4 numbers.
However after that it is returning an error code of 8,meaning
insufficient memory ! This means that only 64bytes of memory have been allocated dynamically.
What may be the reason ?? Please help !
 
Last edited:

Its a long time since I programmed 8086 assembly language but I'm guessing the problem is the memory allocation. I'm not sure you can keep calling 0x48 interrupt functions as a way of extending the memory high water mark. Each time you request more memory, DOS will allocate a new block for you but it may not immediately follow the previous one. As there is no way to identify the blocks when you write data, you may be writing into increasing sequential addresses when the available memory is available is elsewhere in the memory map. I think normal practice would be to allocate a big enough memory block just once rather than in stages. If the number of records is very high I would consider storing on disk instead of RAM.

Brian.
 

Okay ! But then is there no other way of allocating memory in ASM ?N then what happens actually for linked lists implemented in C or C++ ??
N what maybe the reason for insufficient memory error ? There should be lots of other space in RAM !
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top