What should happen is a digit in eax, such as 9, is converted to its character form, ‘9’, and is outputted. I am using nasm on a 32-bit linux machine.
section .data
int: dw 9
section .text
global _start
_start:
mov eax, 9
add eax, 48
mov ecx, [eax]
mov eax, 4
mov ebx, 1
mov edx, 1
int 0x80
mov eax, 1
mov ebx, 0
int 0x80
When running this program, I get a segmentation fault. I debug using gdb and I get a segmentation fault at the line where it says mov ecx, [eax]. Is there any way to fix this?
mov ecx, [eax]means to interpreteaxas a memory address, and copy the data at that location intoecx. This will try to dereference memory at 0x00000039, which is causing the crash.Instead, if you want to copy the value in
eaxintoecx, usemov ecx, eax(without the square brackets).