Assembly 尝试将用户输入作为循环计数器[NASM汇编语言]
我试图运行一个循环,该循环在用户输入的值上运行 数据段:Assembly 尝试将用户输入作为循环计数器[NASM汇编语言],assembly,nasm,Assembly,Nasm,我试图运行一个循环,该循环在用户输入的值上运行 数据段: BITS 32 section .data msg db 'Enter the number of lines ' len equ $-msg hello db 'Hello World ! ' lenhello equ $-hello cr db 10 B.bss部分
BITS 32
section .data
msg db 'Enter the number of lines '
len equ $-msg
hello db 'Hello World ! '
lenhello equ $-hello
cr db 10
B.bss部分
section .bss
num resb 5
.txt段
section .text ;Code Segment
global _start
_start: ;User prompt
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, len
int 80h
mov eax, 3
mov ebx, 2
mov ecx, num
mov edx, 5
int 80h
mov ecx, num ; i think the issue is here
li:
call newline
push ecx
mov eax, 4
mov ebx, 1
mov ecx, hello
mov edx, lenhello
int 80h
pop ecx
loop li
mov eax, 1
mov ebx, 0
int 0x80
换行符改变行
newline: ; this changes the line
push ecx
mov eax, 4
mov ebx, 1
mov ecx, cr
mov edx, 1
int 0x80
pop ecx
ret
当我用一个常量替换num时,代码工作得非常好
mov ecx,5
我认为这与num的数据类型有关。在NASM中,类似于
mov ecx,num的指令将num的地址加载到寄存器中。要获取num指向的地址中存储的内容,需要使用方括号,如mov ecx、[num]
您得到的输入是文本,但您的循环需要其循环计数器的实际数字。您需要进行转换
假设您的输入由一位数字组成,这是一个很好的解决方案:
movzx ecx, byte [num] ; Fetch the single digit input from the buffer
sub cl, '0' ; Conversion "0".."9" -> 0..9
jz Done ; No lines requested
li:
call newline
push ecx
mov eax, 4
mov ebx, 1
mov ecx, hello
mov edx, lenhello
int 80h
pop ecx
dec ecx
jnz li
Done:
在调试器下运行时会发生什么?ecx中的值是多少?猜测一下,您正在从num读取字符串“5”,而不是数字值0x5。@DavidWohlferd:更糟糕的是,NASM中的mov ecx,num
将标签地址放入ecx,mov
-立即不是一个加载(就像在其他一些汇编程序中一样,例如GNU.intel\u syntax
)。