我正在尝试学习汇编(特别是ideone.com上的nasm变种)。当我跳转到一个过程时,我得到错误代码11,而当我刚刚调用该过程时没有错误。我已经尝试过,在块的末尾有“ret”和没有“ret”。请注意,仅当输入长度为2时才调用_printOne过程,例如“a [newline]”。这是我的代码
global _start
section .data
sys_read equ 3
sys_write equ 4
max_line_len equ 10
stdin equ 0
oneStr db '1'
oneStrLen equ $ - oneStr
section .bss
line resb 10
segment .text
_start:
call _readLine ; stores value in line
cmp eax, dword 2 ; if input has length of 2, print out '1'
je _printOne ; No error if "call _printOne"!
mov eax, 01h ; exit()
xor ebx, ebx ; errno
int 80h
_readLine:
mov eax, sys_read ; syscall to read
mov ebx, stdin ; stdin
mov ecx, line ; put line into ecx
mov edx, max_line_len ; length to read
int 0x80
ret
_printOne:
mov eax, sys_write
mov ebx, stdout
mov ecx, oneStr
mov edx, oneStrLen
int 80h
ret
答案 0 :(得分:2)
如果你只是在最后省略RET
,处理器将尝试执行你的代码在内存中的任何垃圾,这可能导致错误。
如果要进行条件调用,只需反转条件并跳过调用,例如:
cmp eax, dword 2
jne skip_print
call _printOne
skip_print:
mov eax, 1
xor ebx, ebx
int 80h
如果你不想让_printOne
进入一个程序,你应该提供一种明智的继续执行方式,例如跳回退出,如下所示:
cmp eax, dword 2
je _printOne
exit:
mov eax, 1
xor ebx, ebx
int 80h
...
_printOne:
mov eax, sys_write
mov ebx, stdout
mov ecx, oneStr
mov edx, oneStrLen
int 80h
jmp exit
最后,建议:不要使用ideone来学习汇编编程。在本地设置一个环境,特别是确保你有一个调试器,你可以单步执行代码并看看发生了什么。