0

Is there any possibility to use RET function to go back from the conditional statement to the main function (_start) where it was called from and go on with the next instructions? Right now, it's just causing a segmentation fault beacuse it is not setted in the stack.

If not, what is the best approach to perform if jumps in asm? Am i supposed to write jmp _cont at the end of the if statement instead of ret and then go on with the instructions in _cont?

section .data

a dq 222
b dq 333

section .text
   global _start

_start:
   
mov rax, [a]

mov rbx, [a]

cmp rax, rbx

je ifstatement1
//desired return point
mov rax, [b]
call _printInt

mov eax, 60
xor edi, edi
syscall

ifstatement1:
 
mov rax, [a]    
call _printInt
ret
6
  • 3
    ret only works if you used call. Since je is a jump, there is no address to return to. Yes, you should use jmp to go to wherever you want to continue execution. If you want to conditionally call a function, you should reverse the condition to skip over a call. Commented Jul 20, 2024 at 13:23
  • 2
    _start is special; isn't a function — the operating system doesn't call it, instead it sets the pc there and runs that code. The usual _start does call main as a real function, so that one can do what you're showing here. Commented Jul 20, 2024 at 13:45
  • No it can't. The question was about the ret in ifstatement1 going back to the desired return point. It would not work in main either. Commented Jul 20, 2024 at 13:56
  • Since the OP just wants to exit after call _printInt, from a function like main they could do a conditional tailcall to _printInt, like je _printInt / set up RAX a different way / jmp _printInt. Commented Jul 20, 2024 at 20:57
  • Your ifstatement1: only has one "caller", so you could just use jmp desired_return_point to a label you placed there, instead of ret. Otherwise you'd need to make a conditional call (e.g. by branching past a call instruction), or inline the (tiny in this case) amount of code at each call-site so there isn't a function, just a block that you jump over or that you jump to and it jumps back to a hard-coded location. Commented Jul 20, 2024 at 21:09

1 Answer 1

2

If you want to jump back to the start of your program, simply do jmp _start. However, if you are asking how to go to the "desired return point" after the execution of the commands located at ifstatement1, you can not use the RET instruction. This is because of how CALL/RET work. The CALL instruction pushes the address of the next instruction to the stack and then jumps to the desired memory location. The RET instruction then pops the pushed address from that stack and jumps to it. However, you are not using the CALL instruction to execute ifstatement1, rather just jumping to it using JE, meaning the stack will not contain the information required for RET to work. The simplest way to solve this would be to do something like

    je ifstatement1
_cont:
    ;rest of your code

ifstatement1:
    ;conditional code
    jmp _cont
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.