注意事项:在x86汇编(16位)中工作;使用Nasm;
当我尝试在DosBox中运行程序时,模拟器冻结(我不确定冻结是正确的词,因为光标仍然闪烁)并拒绝响应输入。我第一次试着运行它的时候,DosBox崩溃了。
下面是我的代码:;ASSIGNMENT 3
org 100h
section.data:
prompt1 db 0dh, 0ah, 0dh, 0ah, "Please input a signed base-10 integer: $"
prompt2 db 0dh, 0ah, "Your number in binary is: $"
prompt3 db 0dh, 0ah, "Pretty sure that wasn't a number. Please enter a number value. $"
section.text:
START:
mov ah, 9 ;Display input prompt
mov dx, prompt1
int 21h
DEC_IN:
mov bx, 0 ;Get input
mov ah, 1
int 21h
cmp al, 0dh ;compare input to carriage return; check if user is finished
je DEC_OUT ;if yes, go display the prompt
cmp al, '0' ;compare to '0'
jg IS_DIGIT ;jump to IS_DIGIT to confirm that it is a number
jl NAN_ERROR ;if below 0, print error prompt and start over
IS_DIGIT:
cmp al, '9' ;confirms digit value
jl BIN_CONV ;if digit, convert to binary
jg NAN_ERROR ;if not, print 'not a number' error message
BIN_CONV:
and al, 0fh ;converts ASCII to binary
neg al ;one's complement
add al, 1 ;add 1 to make two's compliment
jmp ADDIT ;add to bx
ADDIT:
or bl, al ;adds new digit to sum in bx
int 21h
jmp DEC_IN
NAN_ERROR:
mov ah, 9 ;display error message
mov dx, prompt3
int 21h
jmp START ;Go back to beginning
DEC_OUT:
mov ah, 9 ;Display the signed decimal value
mov dx, prompt2
int 21h
如果重要的话,程序应该以无符号十进制值的形式输入,并将其输出为有符号十进制值,然后输出为八进制值。我知道我的程序还没有做到这一点,即使它运行了;它仍处于早期开发阶段。
Thanks in advance
您应该尽可能在"开发早期"学习使用调试器。
也就是说,确保你的数据在代码之后的,否则cpu会尝试将其作为指令执行,这将不是很漂亮。注意,section.data:
是双重错误的,你不能使用冒号,因为它不是一个标签,你必须在点前加一个空格,就像section .data
一样。当然section.text:
也是类似的。如果您这样做,nasm
将足够聪明地将数据放在代码之后。
然而,DOS .com
文件没有节,这只是nasm
的一个方便功能,我不建议使用。