Sum BYTES in a Loop


 
 ; adds byte array in a oop and prints sum
 ; if we assume data elements can be between -128 to 127,
 ; WE COULD HAVE PROBLEMS...
include emu8086.inc
org 100h    

xor ax, ax
xor bx, bx

lea si, x
mov cx, xlen   

SUMLOOP:
  mov bl, byte ptr[si] 
  add ax, bx  
  inc si
loop SUMLOOP  

call print_num
ret

x db 4,5,6,10 ;25
xlen = $-x   
define_print_num
define_print_num_uns
end
 

 
 ; explain changes that FIX POTENTIAL PROBLEM
include emu8086.inc
org 100h    

xor ax,ax

lea si, x
mov cx, xlen   

SUMLOOP:
  mov bh, 0
  test byte ptr[si], 1000_0000b
  jz GO
  mov bh, 0ffh
  GO:  
  mov bl, byte ptr[si]
  add ax, bx  
  inc si
loop SUMLOOP  

call print_num
ret

x db 4,5,6,-10 ; 5
xlen = $-x   
define_print_num
define_print_num_uns
end