1

I'm using SASM and within it, NASM. Whenever I open run my simple NASM file, it closes in the blink of an eye. Here is my code:

%include "io64.inc"

section .text
global CMAIN
CMAIN:
    mov eax,4

    xor rax, rax
    ret
rkhb
  • 14,159
  • 7
  • 32
  • 60
Katido 622
  • 21
  • 1
  • 7

1 Answers1

1

It also closes because all programs close when they are done running. You can add an infinite loop at the end of execution to stop it from closing. Do something like this:

%include "io64.h"

section .text
global CMAIN
CMAIN:
    mov eax, 4

 endLoop:
    pause              ; don't overheat your CPU as much while busy-waiting
    jmp endLoop

   ; never reached unless you use a debugger to get out of the infinite loop
    xor rax, rax
    ret

This will cause the program to loop at endLoop so it never finishes and closes.

Making a system call that sleeps would be a lot more power-efficient way to make a program never exit. Waiting for keyboard input is another common method in C++ programs on systems that default to running a program in a new window that closes when it's done.

Or set a breakpoint inside your program.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Your first sentence is wrong (so I removed it because the rest of the answer is good). `mov eax,4` is perfectly valid in x86-64, and exactly equivalent to `mov rax,4` except shorter. [Why do x86-64 instructions on 32-bit registers zero the upper part of the full 64-bit register?](https://stackoverflow.com/q/11177137). In fact, NASM will optimize `mov rax,4` into `mov eax,4` when assembling, unless you use `mov rax, strict dword 4` to force the 7-byte `mov r/m64, sign-extended-imm32` encoding. But YASM won't. Similarly, you should use `xor eax,eax` to zero RAX. – Peter Cordes Oct 22 '18 at 05:40