• Uncategorized

About linux : Segmentation-fault-with-a-variable-in-SECTION-DATA

Question Detail

I am trying to learn nasm. I want to make a program that prints “Hello, world.” n times (in this case 10). I am trying to save the loop register value in a constant so that it is not changed when the body of the loop is executed. When I try to do this I receive a segmentation fault error. I am not sure why this is happening.

My code:

SECTION .DATA
print_str: db ‘Hello, world.’, 10
print_str_len: equ $-print_str

limit: equ 10
step: dw 1

SECTION .TEXT
GLOBAL _start

_start:
mov eax, 4 ; ‘write’ system call = 4
mov ebx, 1 ; file descriptor 1 = STDOUT
mov ecx, print_str ; string to write
mov edx, print_str_len ; length of string to write
int 80h ; call the kernel

mov eax, [step] ; moves the step value to eax
inc eax ; Increment
mov [step], eax ; moves the eax value to step
cmp eax, limit ; Compare sil to the limit
jle _start ; Loop while less or equal

exit:
mov eax, 1 ; ‘exit’ system call
mov ebx, 0 ; exit with error code 0
int 80h ; call the kernel

The result:

Hello, world.
Segmentation fault (core dumped)

The cmd:

nasm -f elf64 file.asm -o file.o
ld file.o -o file
./file

Question Answer

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.