Search code examples
assemblyx86masmirvine32

Add integers 20, 30, 50 and 160 using register 8-bitAL in assembly using MOVZX


I am rather confused with how to find the sum of integers using an 8-bit register AL? Any hint would be great. Thank you.

TITLE Add              (AddSub.asm)

; This program adds and subtracts 32-bit integers.
;Problem 1 Add 20, 30, 50,160  al  hint move ax,0 to start
; Last update: 2/1/02

TITLE Add               (AddSub.asm)

INCLUDE Irvine32.inc

.code
main PROC

    mov al,20d      
    add al,30d      
    add al,50d      
    add al,160d 
    call DumpRegs

    call writedec 
    exit
main ENDP
END main

Solution

  • When you add a number to AL and there is an overflow, what you get is the low-order 8 bits of the result in AL, and the Carry Flag is set. If you view the Carry Flag plus the AL register as a 9-bit quantity, it is the sum you are looking for. (And a 9-bit quantity is large enough to represent 260.)

    The instruction "ADC" stands for "Add with Carry", so if you already have AH set to zero, (with MOVZX, perhaps?) you can do ADC AH,0 and this will essentially add the carry flag into AH, and then AX will contain your result.

    There you go, that's a lot more than a hint, it is practically the solution.