user2138149
user2138149

Reputation: 17414

NASM Hello World Segmentation Fault

So yeah, I am tinkering with assembly level programming...

Here is what I have so far.

global _start

section .text
_start:

    mov eax, 4      ; write
    mov ebx, 1      ; stdout
    mov ecx, msg
    mov edx, msg.len
    int 0x80        ; system call


    mov eax, 1      ; exit
    mov ebx, 0      ; exit code
    int 0x80        ; system call

section .data

    msg: db "Hello world!", 10  ; Defines the string "Hello world!\n"
    .len equ $-msg

How does the statement .len equ $-msg work? I understand this is the length of the string. I also know that equ is like #define in C. So this variable does not exist in memory, it is put in place by the assembler. (nasm)

What does the $ symbol do, and is that a subtraction occurring afterwards?

My output causes a segfault, I am hoping I will be able to fix this myself when I understand the .len equ $-msg syntax. I have fixed the fault, but still don't understand the $ notion.

EDIT Segfault caused by this being a malformed program. Fixed

Upvotes: 0

Views: 1151

Answers (1)

lurker
lurker

Reputation: 58324

$ represents the address of the current line. So the following:

.len  equ  $-msg

Means the current address minus the address of msg. That gives the length of the data stored between msg and .len (since the address of .len is represented by $). Thus, the symbol .len represents (equated to) that length value.

Upvotes: 2

Related Questions