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 3 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 !

.model small

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

    mov bx,1
    mov ah,48h
    int 21h

    mov head,ax

    mov dx,offset m1
    mov ah,09h
    int 21h
    mov ah,01h
    int 21h
    xor ah,ah
    mov bx,head
    mov [bx],ax
    mov di,head

    mov dx,offset m2
    mov ah,09h
    int 21h
    mov ah,01h
    int 21h
    cmp al,'y'
    je loop1
    cmp al,'Y'
    je loop1
    jmp below
       
loop1:
    mov bx,1
    mov ah,48h
    int 21h

    xor si,si
    mov [di+2],ax
    xor di,di
    mov si,ax
    mov dx,offset m1
    mov ah,09h
    int 21h
    mov ah,01h
    int 21h
    xor ah,ah
    mov word ptr [si],ax
    mov di,si
    
    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
    
disp:
    mov ah,02h
    mov dx,10
    int 21h
    mov dx,13
    int 21h
    mov bx,head
    mov ah,02h
    mov dx,[bx]
    int 21h

lp:
    mov ah,02h
    add bx,2
    mov dx,word ptr [bx]
    int 21h
    cmp word ptr [bx+2],0
    jnz lp

exit:
    mov ah,4ch
    int 21h

end

Here's the code with comments,

.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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.