How to use ADC in assembly?

.MODEL SMALL
.STACK 1000
.DATA

MSGA DB 13,10,"Input first number: ","$"
MSGB DB 13,10,"Input second number:","$"
MSGC DB 13,10,"The sum is: ","$"

NUM1 db ?
NUM2 db ?
NUM3 db ?

.CODE

MAIN PROC NEAR

MOV AX, @DATA
MOV DS, AX

; get first number
LEA DX, MSGA
MOV AH, 09h
INT 21h

MOV AH, 01
INT 21H
SUB AL, '0'

MOV BL, AL

MOV AH, 01
INT 21H
SUB AL, '0'

MOV CL, AL

; get second number
LEA DX, MSGB
MOV AH, 09h
INT 21h

MOV AH, 01
INT 21H
SUB AL, '0'

MOV DL, AL

MOV AH, 01
INT 21H
SUB AL, '0'

MOV DH, AL

; add
ADD CL, DH 
ADC BL, DL
MOV NUM1, CL
ADD NUM1, '0'
MOV NUM2, BL
ADD NUM2, '0'

; output sum
LEA DX, MSGC
MOV AH, 09h
INT 21h

MOV DL, NUM2
MOV AH, 02H
INT 21h

MOV DL, NUM1
MOV AH, 02H
INT 21h

MOV AH, 4Ch
INT 21h

MAIN ENDP
END MAIN

Above is my code for adding 2 double-digit numbers in an assembly. I wonder why the ADC is not working. If these numbers don't get wrapped when added, my code works. But not otherwise. Did I not understand what the ADC really does? What should I do with my code?

+5
source share
2 answers

You seem to be working with decimal math, but you are not using AAA. ADCdoes what you expect, but with this code there is never a transfer from the first addition (after all, 9 + 9 is no more than 255).

So, the solution, of course, should use AAAit like this (not verified):

add al,dl
aaa        ; takes no arguments and works on al
add ah,dh  ; adc not necessary, aaa already incremented ah if there was a carry

AAA( ASCII Adjust for Addition ) if algreater than 9, and if so:

  • al,
  • ah
+4

fooobar.com/questions/427949/..., , ADC - Add with carry. , ADD. , ADC (add with carry), , .

, AX BX, :

add  al, bl
adc  ah, bh   ; include the carry if previous add generated one

, , ASCII, "0". 0 9, 8- . , , 10, , , 256.

, , , , 10, 10 1 .

?

? , , , ?

+1

All Articles