写一个程序来加2个数字

时间:2017-03-21 23:14:11

标签: assembly emu8086

您好我要写一个练习来将0到9之间的2个数字相加,问题是当我之后只能得到15个结果时,如果结果为17,它将返回0将显示1。

我做错了什么?

#make_COM#

; COM file is loaded at CS:0100h
ORG 100h

;*******************************
;*    Programa: CONDIC4.ASM    *
;*******************************

.MODEL small
.STACK 512d

.DATA
  msg1 DB 'Decimal 1 (0 to 9): ', 024h
  msg2 DB 0Dh, 0Ah, 'Decimal 2 (0 to 9): ', 024h
  msg3 DB 0Dh, 0Ah, 'Sum = ', 024h
  msg4 DB 0Dh, 0Ah, 'Invalid', 024h

.CODE
  LEA   DX, msg1
  CALL  write
  CALL  read
  MOV   BH, AL

  LEA   DX, msg2
  CALL  write
  CALL  read
  MOV   BL, AL

  LEA   DX, msg3 
  CALL  write
  XCHG  AX, BX

  ADD   AL, AH
  SUB   AH, AH
  AAA
  MOV   DX, AX
  MOV   AH, 0Eh
  CMP   DH, 0h
  JE    not_zero
  OR    DH, 30h
  MOV   AL, DH
  INT   010h
  not_zero:
  OR    DL, 30h
  MOV   AL, DL
  INT   010h
  INT   020h

write PROC NEAR
  MOV   AH, 09h
  INT   021h
  RET
write ENDP

read PROC NEAR
  MOV   AH, 01h
  INT   021h
  CMP   AL, 030h
  JL    erro
  CMP   AL, 03Ah
  JGE   erro
  SUB   AL, 030h  
  RET
  erro:
    LEA   DX, msg4
    CALL  write
    INT   020h
  RET
read ENDP

1 个答案:

答案 0 :(得分:1)

我使用MOV(MOV AH,0h)命令而不是SUB(SUB AH,AH)修正了它,因为手册说MOV不会改变任何标志。

#make_COM#

; COM file is loaded at CS:0100h
ORG 100h

;*******************************
;*    Programa: CONDIC4.ASM    *
;*******************************

.MODEL small
.STACK 512d

.DATA
  msg1 DB 'Decimal 1 (0 to 9): ', 024h
  msg2 DB 0Dh, 0Ah, 'Decimal 2 (0 to 9): ', 024h
  msg3 DB 0Dh, 0Ah, 'Sum = ', 024h
  msg4 DB 0Dh, 0Ah, 'Invalid', 024h

.CODE
  LEA   DX, msg1
  CALL  write
  CALL  read
  MOV   BH, AL

  LEA   DX, msg2
  CALL  write
  CALL  read
  MOV   BL, AL

  LEA   DX, msg3 
  CALL  write
  XCHG  AX, BX

  ADD   AL, AH  
  MOV   AH, 0h     ;Clears AH WITHOUT CHANGING FLAGS
  AAA
  MOV   DX, AX
  MOV   AH, 0Eh
  CMP   DH, 0h
  JE    not_zero
  OR    DH, 30h
  MOV   AL, DH
  INT   010h
  not_zero:
  OR    DL, 30h
  MOV   AL, DL
  INT   010h
  INT   020h

write PROC NEAR
  MOV   AH, 09h
  INT   021h
  RET
write ENDP

read PROC NEAR
  MOV   AH, 01h
  INT   021h
  CMP   AL, 030h
  JL    erro
  CMP   AL, 03Ah
  JGE   erro
  SUB   AL, 030h  
  RET
  erro:
    LEA   DX, msg4
    CALL  write
    INT   020h
  RET
read ENDP