0

This works but I don't know what the last section does. If I remove it the code segfaults, so I'm assuming it is somehow related to closing the file or handling memory allocation but as far as I am aware that what the 6 instruction is for.

section .data
    msg db 'Hello, world!', 0xa
    len equ $ - msg
    outfile db 'test_file.txt', 0
section .text
    global _start

_start:

;   creating and opening
    mov eax, 8          ; set instruction
    mov ebx, outfile    ; set file name
    mov ecx, 544o       ; set file permissions
    int 0x80            ; make system call
    
;   writing to file
    mov ebx, eax        ; move file descriptor from previous call
    mov eax, 4          ; set instruction
    mov ecx, msg        ; set text
    mov edx, len        ; set number of bytes to read
    int 0x80            ; make system call

;   closing file
    mov eax, 6          ; set instruction
    int 0x80            ; make system call

;   closing program?
    mov eax,1           
    mov ebx,0
    int 0x80
zx485
  • 28,498
  • 28
  • 50
  • 59
P O
  • 13
  • 4
  • Not sure what your question is, it's commented well enough. It's a `close()` (syscall #6) and an `exit()` (syscall #1). You do not need to close the file as the OS will do it for you. It's good practice anyway. You do need to terminate the program however, as you can't just fall off the end. – Jester Mar 16 '22 at 17:03
  • the question was regarding the exit call. I figured that's what it was but i couldn't find that explicitly anywhere – P O Mar 16 '22 at 17:09
  • The processor doesn't know where the program ends, or when you want to end it. The proper approach is to ask the operating system to terminate the program at such time as it is done. If you are writing on bare metal, sometimes there is a halt instruction, on some systems we simply put a jump to self to create a harmless infinite loop. – Erik Eidt Mar 16 '22 at 17:15
  • `__NR_exit` is defined as `1` in `asm/unistd_32.h`. Or run it under `strace`. See [Hello, world in assembly language with Linux system calls?](https://stackoverflow.com/q/61519222) for a fully-explained hello world. – Peter Cordes Mar 16 '22 at 17:21

0 Answers0