Windows/DOS汇编 - 简单数学

3

我目前正在学习Windows/DOS汇编语言。我正在制作一个小程序,它可以将两个十进制整数相加,并将答案输出到标准输出。这是我的当前代码:

org 100h


MOV al,5
ADD al,3

mov dx,al

mov ah,9
int 21h

ret

我不明白为什么编译时会出现以下错误信息:

error: invalid combination of opcode and operands

。理论上,我只是将数字5放入AL寄存器中,加上3,将AL寄存器的内容放入DX寄存器进行输出,然后显示结果。请帮忙解决这个问题,谢谢!
1个回答

7

DX 是一个16位的寄存器,而 AL 是一个8位的。

AL 装入 DL,并将 DH 设置为0。

但这样做不会达到你想要的效果;函数9 [显示以空字符结尾的字符串]。你正在告诉它显示从数据段的偏移量9开始的字符串,这可能是垃圾。

你需要先将答案转换为一系列数字,然后调用函数9。

将寄存器内容转换为字符串 的一些示例代码可供参考。以下是由用户 Bitdog 编写的代码。

; ------ WDDD = Write a Decimal Digit Dword at the cursor & a CRLF ------
;
; Call with,    DX:AX = Dword value to print
; Returns,  CF set on error, if DX:AX > 655359 max#
;       (see WDECEAX.inc for larger number prints)
align 16
WDDD:   CMP DX,10
    JB  WDDDok
    STC     ;CF=set
    RET     ;error DX:AX exceeded max value
WDDDok: PUSH    AX
    PUSH    BX
    PUSH    CX
    PUSH    DX
    XOR CX,CX   ; clear count register for push count
    MOV BX,10
WDDDnz: DIV BX  ; divide DX:AX by BX=10
    PUSH    DX  ; put least siginificant number (remainder) on stack
    XOR DX,DX   ; clear remainder reciever for next divide
    OR  AX,AX   ; check to see if AX=number is divided to 0 yet
    LOOPNE  WDDDnz  ; get another digit? count the pushes
    MOV AH,2    ; function 2  for interupt 21h write digit
    NEG CX  ; two's compliment, reverse CX
    MOV BL,48   ; '0'
WDDDwr: POP DX  ; get digit to print, last pushed=most significant
    ADD DL,BL   ; convert remainder to ASCII character
    INT 21h ; print ascii interupt 21h ( function 2 in AH )
    LOOP    WDDDwr  ; deincrement CX, write another, if CX=0 we done
    MOV DL,13   ; CR carriage return (AH=2 still)
    INT 21h
    MOV DL,10   ; LF line feed
    INT 21h
    POP DX
    POP CX
    POP BX
    POP AX
    CLC     ;CF=clear, sucess
    RET

; A divide error occurs if DX has any value
; when DIV trys to put the remainder into it
; after the DIVide is completed.
; So, DX:AX can be a larger number if the divisor is a larger number.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接