Assembly 此代码是否正确(数字加数字,然后打印结果)
我想用汇编语言做一些简单的事情。Assembly 此代码是否正确(数字加数字,然后打印结果),assembly,x86,dos,Assembly,X86,Dos,我想用汇编语言做一些简单的事情。 将两个数字相加,并在屏幕上打印结果 我做了那个代码: .Model SMALL .Stack 100h .Code start: MOV ax, 10 ADD ax, 5 MOV ah, 02h INT 21h MOV ah, 01h INT 21h MOV ah, 4ch INT 21h end start 编译完代码后没有任何错误,告诉我一个奇怪的字符 修改: MOV dl, 10 ADD al,
将两个数字相加,并在屏幕上打印结果 我做了那个代码:
.Model SMALL
.Stack 100h
.Code
start:
MOV ax, 10
ADD ax, 5
MOV ah, 02h
INT 21h
MOV ah, 01h
INT 21h
MOV ah, 4ch
INT 21h
end start
编译完代码后没有任何错误,告诉我一个奇怪的字符
修改:
MOV dl, 10
ADD al,5
MOV dl, al
MOV ah,02h
INT 21h
但仍然打印一个奇怪的字符
我不知道如何在屏幕上打印数字是的,您很可能会得到一个奇怪的字符,因为int 21/ah=02要求打印的字符必须在dl
寄存器中,并且您没有用任何东西填充dl
您可能希望通过以下方式传递值:
mov ax, 10
add ax, 5
push ax ; these are the two new lines.
pop dx
mov ah, 02h
但是,请记住,即使将值从al
传输到dl
,字符号15也可能不是您所期望的。15是ASCII控制字符之一,我不确定DOS中断会为它们输出什么
如果要打印数字15
,需要两个调用,一个调用dl=31h
,第二个调用dl=35h
(两个ASCII码分别用于1
和5
字符)
如果您想知道如何在寄存器中获取一个数字,并以可读的形式输出该数字的数字,那么其中有一些伪代码
根据该答案,您可以得到伪代码:
val = 247
units = val
tens = 0
hundreds = 0
loop1:
if units < 100 goto loop2
units = units - 100
hundreds = hundreds + 1
goto loop1
loop2:
if units < 10 goto done
units = units - 10
tens = tens + 1
goto loop2
done:
if hundreds > 0: # Don't print leading zeroes.
output hundreds
if hundreds > 0 or tens > 0:
output tens
output units
;; hundreds = 2, tens = 4, units = 7.
现在,代码段(尽管由于我做汇编已经有一段时间了,所以有任何错误)应该打印出百位数字,而ax只保留十位和单位数字
将功能再复制两次以获得十位数和单位位数应该很简单。不幸的是,在应用代码后,问题仍然存在。@Lion,请参阅“但是,请记住…”开头的一段。你到底想打印什么?如果是字符串15,我已经添加了最后一段,告诉你需要做什么。我已经给了你足够多的东西,只要你自己努力一点,就能实现你想要的。所以不是一个“给我密码”的网站。如果你想让某个仆人帮你写代码,我建议你去elance或rentacoder看看并付钱。首先,我感谢你继续帮助我,我并不是说我想让一个仆人帮你写代码,但我不了解任何信息,除非只有example@Lion:好的,首先要做的事。您想要输出什么?你还没告诉我们。
mov ax, 247 ; or whatever (must be < 1000)
push ax ; save it
push dx ; save dx since we use it
mov dx, 0 ; count of hundreds
loop1:
cmp ax, 100 ; loop until no more hundreds
jl fin1a
inc dx
sub ax, 100
jmp loop1
fin1a:
add dx, 30h ; convert to character in dl
push ax ; save
mov ah, 2
int 21h ; print character
pop ax ; restore value
; now do tens and units the same way.
pop dx ; restore registers
pop ax