简体   繁体   中英

How to display the summation of two negative number in HEX while the input is decimal in x8086 assembly language?

; Hello, I'm new to assembly language x8086, the problem is how can I add two negative numbers (decimal) and display them in HEX. I'm stuck at the point the code does not detect the negative numbers and I don't know how to display it in HEX. can someone help me? here my code

TITLE PROB12
PAGE 60,132
.MODEL SMALL
.STACK 64H
.DATA
NUM1 DB 8,?,8 DUP (?)
NUM2 DB 8,?,8 DUP (?)
PROMPT1 DB CR,LF,'Enter the first number','$'
PROMPT2 DB CR,LF,'Enter the second number','$'
PROMPT3 DB CR,LF,'The total sum is '
SUM DB 7 DUP (?),'$'
CR EQU 0DH
LF EQU 0AH
.CODE
MAIN: MOV AX,@DATA
MOV DS,AX
CALL CLEAR ;clear screen
MOV AH,09
MOV DX,OFFSET PROMPT1
INT 21H ;display first prompt
MOV AH,0AH
MOV DX,OFFSET NUM1
INT 21H ;get first number
MOV AH,09
MOV DX,OFFSET PROMPT2
INT 21H ;display second prompt
MOV AH,0AH
MOV DX,OFFSET NUM2
INT 21H ;get second number
MOV SI,OFFSET NUM1 + 8 ;point to LSD of number 1
MOV DI,OFFSET NUM2 + 8 ;point to LSD of number 2
MOV BX,OFFSET SUM + 6 ;point to LSD of sum
MOV CX,7 ;add 7 bytes
CLC ;clear carry
ADD_LP: MOV AL,[SI] ;get byte from number 1
ADC AL,[DI] ;add byte from number 2
PUSHF ;save any carry
AAA ;ASCII adjust
OR AL,30H ;make it ASCII
POPF ;restore flags
MOV [BX],AL ;store sum (in BCD)
DEC SI ;decrement pointers
DEC DI ;to point to next byte
DEC BX
LOOP ADD_LP ;loop through 7 bytes
MOV AH,09
MOV DX,OFFSET PROMPT3
INT 21H ;display result
MOV AH,4CH
INT 21H ;go back to DOS
;--------------------------------------------------
CLEAR PROC
MOV AH,06 ;clear screen function
MOV AL,00 ;page 0
MOV BH,07 ;normal attribute
MOV CX,0 ;entire screen
MOV DX,184FH
INT 10H
RET
CLEAR ENDP
END MAIN

I'm stuck at the point the code does not detect the negative numbers and I don't know how to display it in HEX

<\/blockquote>

This approach can only work if the user at the keyboard always provides precisely 7 digits<\/strong> . That's not very user-friendly.

Currently you are preserving the carry from the adc<\/code> operation, but that's pointless since it will always be clear. You need to propagate throughout the loop the carry that you obtain from the aaa<\/code> instruction<\/strong> .

 If both are indeed present then the current addition is fine as long as you iterate over the remaining 6<\/strong> digits (7-1) and that you write a "-" character at SUM<\/em> .

"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM