gpt4 book ai didi

Windows/DOS 汇编 - 简单数学

转载 作者:可可西里 更新时间:2023-11-01 14:44:38 27 4
gpt4 key购买 nike

我目前正在学习 Windows/DOS 汇编。我只是在制作一个小程序,将两个以 10 为底的整数相加,并将结果输出到标准输出。这是我当前的代码:

org 100h


MOV al,5
ADD al,3

mov dx,al

mov ah,9
int 21h

ret

我很困惑为什么在编译时出现错误:

错误:操作码和操作数的组合无效

因为理论上我就是把5放到AL寄存器里,加上3,把AL寄存器的内容取出来放到DX寄存器里输出,然后显示出来。

任何帮助将不胜感激,谢谢!

最佳答案

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

AL载入DL,并设置DH为0。

但这不会做你想做的;函数 9 [显示以 null 结尾的字符串]。您告诉它显示一个从数据段的偏移量 9 开始的字符串,这可能是垃圾。

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

converting the contents of a register to a string 有一些示例代码可用的。复制于此以供引用,由别名为 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.

关于Windows/DOS 汇编 - 简单数学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1297808/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com