I am writing an assembly program in NASM and trying to write the value of 2 memory values to a file (specifically the value of varDD and varDW).
Unfortunately, I get a segmentation fault when trying to run it. Can anyone help identify the issue with this?
Please note that I'm not an assembly expert by any means so please forgive my ignorant mistakes.
section .data
fileName db './output.txt', 0
varDD dd 1.01, 0x00
varDW dw 'abc', 0x00
varDDL equ $ - varDD
varDWL equ $ - varDW
section .text
global main
main:
mov EAX, 8
mov EBX, fileName
mov ECX, 0700
int 0x80
mov EBX, EAX
mov EAX, 4
mov ECX, [varDD]
mov EDX, [varDDL]
int 0x80
mov ECX, [varDW]
mov EDX, [varDWL]
int 0x80
mov EAX, 6
int 0x80
mov eax, 1
int 0x80
mov eax, 60
mov edi, 0
syscall
Update: So I corrected to the following code:
section .data
fileName db './output.txt', 0
varDD dd 1.01, 0x00
varDW dw 'abc', 0x00
varDDL equ $ - varDD
varDWL equ $ - varDW
section .text
global main
main:
mov EAX, 8
mov EBX, fileName
mov ECX, 0700
int 0x80
mov EBX, EAX
mov EAX, 4
mov ECX, varDD
mov EDX, varDDL
int 0x80
mov EAX, 4
mov ECX, varDW
mov EDX, varDWL
int 0x80
mov EAX, 6
int 0x80
mov eax, 1
int 0x80
mov eax, 1
mov ebx, 0
int 0x80
This does run and output a file but when viewing the hexdump of the file, the contents do not correspond to the hex values expected for the variables that were written.
Hex dump: 0000000 47ae 3f81 0000 0000 6261 0063 0000 6261 0000010 0063 0000 0000014
Update 2: I updated as follows:
section .data
fileName db './output.txt', 0
varDD dd 1.01, 0x00
varDDL equ $ - varDD
varDW dw 'abc', 0x00
varDWL equ $ - varDW
The strace (relevant section) shows below:
creat("./output.txt", 01274) = 3
write(3, "\256G\201?\0\0\0\0", 8) = 8
write(3, "abc\0\0\0", 6) = 6
close(3) = 0
I am supposed to use a hexdump to verify the values, but the hexdump still shows:
0000000 47ae 3f81 0000 0000 6261 0063 0000
000000e
That doesn't seem to correspond with the values that were written. 62 = b 61 = a and 63 = c .. those are out of order and I am not seeing the 1.01 value.