assemblyx86masm

my assembly code doesnt run and i dont understand why


The assembly code initializes arrays A and B, calculates the element-wise sum of these arrays, and stores the results in array C. It also provides a procedure, printarray, to print the entire contents of an array and uses this procedure to print the values of arrays A, B, and C to the console. The code assumes a DOS-like environment and requires an x86-compatible system with appropriate assembly tools for execution. yet it didnt give me any result and when i tried debugging it exited the program

.model  small
.stack   100h
.data
A dw 12, 5, 8, -1, 4
B dw -2, 9, 0, 18, 3
C dw 5 dup (?)
N dw 5
.code
mov ax,@data
mov ds,ax
push offset A
push offset B
push offset C
push N
call arraysum
push offset A
push N
call printarray
mov ah, 2
mov dl, 10   
int 21h
push offset B
push N
call printarray
mov ah, 2
mov dl, 10   
int 21h
push offset C
push N
call printarray
mov ah, 2
mov dl, 10   
int 21h
.exit
sum proc near
push bp
mov bp,sp
mov ax,[bp+4]   
add ax,[bp+6]
pop bp
ret 4
sum endp
arraysum proc near
push bp
mov bp, sp
mov cx,[bp+4]
mov di, [bp + 10]
mov si, [bp + 8]
mov bx,[bp+6]
next:
push word ptr [di]
push word ptr [si]
call sum
pop word ptr [di]
pop word ptr [si]
mov [bx], ax
add si, 2  
add di, 2  
add bx, 2
dec cx
jnz next
pop bp
ret 8
arraysum endp

printnum proc near
    push bp
    mov bp, sp
    mov ax, [bp + 4] 
    mov bx, 10
    mov cx, 0
    
    cmp ax, 0 
    jge next
    neg ax
    
next1:   
    mov dx, 0
    div bx
    add dx, 30h
    push dx
    inc cx
    cmp ax, 0
    jne next1
    
    mov ax,[bp+4]
    cmp ax, 0
    jge sof
    push '-'
    inc cx
    
sof:
    cmp cx, 0
    jz ext
    pop dx
    mov ah, 2
    int 21h
    dec cx
    jmp sof
    
ext:
    pop bp
    ret 2   
printnum endp
printarray proc near
    push bp
    mov bp, sp
    mov cx, [bp + 4]  
    mov si, [bp + 6]  
    
print_loop:
    push word ptr [si]  
    call printnum       
    add sp, 2          
    inc si            
    dec cx            
    jnz print_loop     

    pop bp
    ret 4
printarray endp


end

Solution

  • I changed/added few things.

    1. OPTION NOKEYWORD:<C>, compiler no longer complains that this is keyword.
    2. Linespush offset A, B or C work when directives .186,.286,.286c or.286p are applied before .model. In other cases program compiles but doesn't print result on the screen or hangs. When .386 is after .model works as well. (https://stackoverflow.com/a/62603303/20889367)
    3. In sum proc, line ret 4 should be ret because after sum proc program pops values and ret 4 changed stack pointer to wrong location.
    4. Order of instructions after call to sum proc, should be pop word ptr [si], pop word ptr [di].
    5. printarray proc, value in cx is destroyed inside printnum proc so I added push cx / pop cx.
    6. In printnum proc I added 2 bytes to offset in lines mov ax, [bp + 6] because of the above cx instructions and procedure ends with ret not ret 2.
    7. Program ends correctly ax = 4c00h, int 21h.
    8. Code prints ' ' between values.

    Result:

    12 5 8 -1 4
    -2 9 0 18 3
    10 14 8 17 7
    

    Code:

    OPTION NOKEYWORD:<C>
    
    .286
    .model  small
    .stack   100h
    
    .data
        A dw 12, 5, 8, -1, 4        
        B dw -2, 9, 0, 18, 3        
        C dw 5 dup (?)              
        N dw 5                      
        
    .code
    
    start:
        mov ax,@data
        mov ds,ax
    
    ;   mov dx,offset A
    ;   push dx
    ;   mov dx, offset B
    ;   push dx
    ;   mov dx,offset C
    ;   push dx
    
        push offset A
        push offset B
        push offset C
        push N
    
        call arraysum
    
    ;   mov dx, offset A
    ;   push dx
    
        push offset A
        push N
    
        call printarray
    
        mov ah, 2
        mov dl, 10   
        int 21h
    
    ;   mov dx, offset B
    ;   push dx
    
        push offset B
        push N
    
        call printarray
    
        mov ah, 2
        mov dl, 10   
        int 21h
    
    ;   mov dx, offset C
    ;   push dx
    
        push offset C
        push N
    
        call printarray
    
        mov ah, 2
        mov dl, 10   
        int 21h
    
    exit:
        mov ax,4c00h
    int 21h
    
    sum proc near
        push bp
        mov bp,sp
        mov ax,[bp+4]   
        add ax,[bp+6]
        pop bp
        ret                 
    sum endp
    
    arraysum proc near
        push bp
        mov bp, sp
        mov cx,[bp+4]           ;; num rep
        mov di, [bp + 10]       ;; off array A
        mov si, [bp + 8]        ;; off array B
        mov bx, [bp+6]          ;; off array C 
    
    next:
        push word ptr [di]      ;; push elements A[i] and B[i], i = 0 .. 4
        push word ptr [si]
        
        call sum
        
        pop word ptr [si]
        pop word ptr [di]       
                              
        mov [bx], ax
        add si, 2  
        add di, 2  
        add bx, 2
        dec cx
        jnz next
        pop bp
        ret 8           
    arraysum endp
    
    printnum proc near
        push bp
        mov bp, sp
        mov ax, [bp + 6]            
        mov bx, 10
         
        mov cx, 0                   
        
        cmp ax, 0 
        jge next1
        neg ax
        
    next1:   
        mov dx, 0
        div bx
        add dx, 30h
        push dx
        inc cx
        cmp ax, 0
        jne next1
        
        mov ax,[bp + 6]
        cmp ax, 0
        jge sof
         xor ax,ax
         mov al,'-'
        push ax
        inc cx
        
    sof:
        cmp cx, 0
        jz ext
        pop dx
        mov ah, 2
        int 21h
        dec cx
        jmp sof
        
    ext:
        pop bp
        
        ret         ;
    printnum endp
    
    printarray proc near
        push bp
        mov bp, sp
        mov cx, [bp + 4] 
        mov si, [bp + 6] 
        
    print_loop:
        push word ptr [si]  
           push cx 
        call printnum
    pop cx  
        add sp, 2          
        add si,2               
        dec cx     
    
        mov ah, 2
        mov dl, ' '   
        int 21h
        
        jnz print_loop     
    
        pop bp
        ret 4
    printarray endp
    
    end start