Reputation: 1804
I'm trying to learn assembly (specifically the nasm variety on ideone.com). When I jump to a procedure I'm getting error code 11, while when I just call the procedure there is no error. I've tried it with and without the "ret" at the end of the block. Note that the _printOne procedure is only called if the input is of length 2, e.g "a[newline]". Here is my code
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
Upvotes: 0
Views: 74
Reputation: 58822
If you simply leave out the RET
at the end, the processor will try to execute whatever garbage is after your code in memory, that's probably causing the fault.
If you want to make a conditional call, just reverse the condition and jump over the call, such as:
cmp eax, dword 2
jne skip_print
call _printOne
skip_print:
mov eax, 1
xor ebx, ebx
int 80h
If you don't want to make _printOne
into a procedure, you should provide a way for execution to continue sensibly, such as by jumping back to exit, as follows:
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
Finally, an advice: do not use ideone to learn assembly programming. Set up an environment locally, in particular make sure you have a debugger where you can single step your code and see what is happening.
Upvotes: 2