Home > Net >  Subtraction of Two Arrays and Stores Result in 3rd array and display the result on screen in Assembl
Subtraction of Two Arrays and Stores Result in 3rd array and display the result on screen in Assembl

Time:02-19

I have already written the code to add TWO arrays and Store the result in 3rd array. But the problem occurs while handling the NEGATIVE SIGN Numbers to display with (-) sign. Follow are the code listed below while subtracting the 6th element of array1 with array 2, result is GARBAGE value Need assistance immediately. After running executing's the code, all signed values are not displaying correctly.

 org 100h
 Array1 db 1,3,2,2,2,2,2,2,2,2
 Array2 db 4,5,6,7,8,9,0,1,2,3  
 Array3  db  10 dup (?)


 lea dx, msg1
 mov ah, 9
 int 21h 

 mov cx, 10
 mov bx, 0

 L1001:

  mov     al, Array1 [bx]
  ; Extend (unsigned) AL to AX (to print)    
  mov     ah, 0
  call    printd
 
  mov     ah, 2
  mov     dl, 09 ;TAB Character
  int     21h

  inc     bx
 loop    L1001       ;End Loop1
 
 mov ah,2
 mov dl,10
 int 21h
 mov dl,13
 int 21h

 ; print msg2     
 lea     dx, msg2
 mov     ah, 9
 int     21h

 ; Use loop to print values of Array2                
 mov     cx, 10
 mov     bx, 0       

 L1002:

  mov     al, Array2 [bx]
  ; Extend (unsigned) AL to AX (to print)    
  mov     ah, 0
  call    printd
  
  mov     ah, 2
  mov     dl, 09 ;TAB Character
  int     21h
  inc     bx
loop    L1002   End Loop2

 mov ah,2
 mov dl,10
 int 21h
 mov dl,13
 int 21h

; print msg3    
lea     dx, msg3
mov     ah, 9
int     21h     

mov cx,10
mov bx, 0

L1003:                          ; Main Addition
 mov     al, Array1 [bx]
 sub     al, Array2 [bx]
 mov     Array3 [bx], al
 ; Extend (unsigned) AL to AX (to print)    
 mov     ah, 0
 call    printd

 mov     ah, 2
 mov     dl, 09 ;TAB Character
 int     21h   

 inc     bx
loop    L1003  ; End of lOOP3  


 lea dx, pkey
 mov ah, 9
 int 21h        ; output string at ds:dx

 ; wait for any key....    
 mov ah, 1
 int 21h

 mov ax, 4c00h ; exit to operating system.
 int 21h

 printd  proc

  ; preserve used registers
  push    ax  
  push    bx
  push    cx    
  push    dx

 ; if negative value, print - and call again with -value     
 cmp     ax, 0
 jge     L1

 mov     bx, ax

 ; print -    
 mov     dl, '-'
 mov     ah, 2
 int     21h   

 ; call with -AX             
 mov     ax, bx
 neg     ax
 call    printd
 jmp     L3

L1:

 ; divide ax by 10
 ; ( (dx=0:)ax / cx(= 10) )
 mov     dx, 0
 mov     cx, 10
 div     cx

 ; if quotient is zero, then print remainder              
 cmp     ax, 0
 jne     L2 
    
add dl, '0'
mov ah, 2
int 21h
jmp L3

L2: 
 ; if the quotient is not zero, we first call
 ; printd again for the quotient, and then we
 ; print the remainder.

 ; call printd for quotient:
 call    printd             

 ; print the remainder
 add     dl, '0'
 mov     ah, 2
 int     21h        

L3:
 ; recover used registers
 pop     dx
 pop     cx
 pop     bx
 pop     ax
 ret
printd  endp 

printud  proc ;Print Undecimal Numbers
 push    ax  
 push    bx
 push    cx    
 push    dx

 mov     dx, 0
 mov     cx, 10
 div     cx

 cmp     ax, 0
 jne     L4

 add     dl, '0'
 mov     ah, 2
 int     21h

 jmp     L5

L4:
  call    printud
  add     dl, '0'
  mov     ah, 2
  int     21h        

L5:
  pop     dx
  pop     cx
  pop     bx
  pop     ax
  ret
printud  endp ; 

ret  
 msg1    db  "Array 1 = $"
 msg2    db  "Array 2 =  $"
 msg3    db  "Array 3 =      : $"
 pkey    db "press any key...$" 



                                           

CodePudding user response:

mov     al, Array1 [bx]
sub     al, Array2 [bx]
mov     Array3 [bx], al
; Extend (unsigned) AL to AX (to print)    
mov     ah, 0
call    printd

You say that your program is having trouble displaying the negative numbers, but your code is never feeding any negative number to the printd routine! Whatever the signed result of the subtraction in AL may be, the mov ah, 0 that follows will produce a positive number in AX and it is AX that printd processes...
You should replace mov ah, 0 by cbw.

But, what is terribly wrong is the placement of the 3 arrays. They can't be at the top of the program like that. The cpu is executing their bytes (data) as if it were instructions (code)!
Move those 3 lines towards the bottom of the source.

It surprised me to see these recursive solutions to display a decimal number. I believe they are correct with one exception though! If you feed printd the negative number -32768 then the program will fall into an infinite loop. This happens because the negation of that particular value is again -32768.
You might want to investigate Displaying numbers with DOS for info about the iterative solution that is surely faster (and could be improved further by outputting the digits all at once).

  • Related