JJHH
JJHH

Reputation: 77

Encoding problem when writing to a file in x86

I'm a new to x86 and currently writing a program where I need to write a file after doing some operations with data I read from a file, but I am facing problems when I write the result to a file, because it is writing with some weird enconding.

Here I reserve the space I am going to need to store the result and the output file:

section .data
     new_file db "new_file.txt", 0 

section .bss
     data resb 4

The code that writes data to a file called new_file.txt:

mov rax, SYS_OPEN
mov rdi, new_file
mov rsi, O_CREAT + O_WRONLY
mov rdx, 0644o
syscall

push rax
mov rdi, rax
mov rax, SYS_WRITE
mov rsi, data
mov rdx, 4
syscall

mov rax, SYS_CLOSE
pop rdi
syscall

For example, assume that I want to do some addition and then store the result in data to write it later:

mov rax, 0xF
add rax, 0x1
mov [data], rax

in this case the data would have a value of 0x10 and when I check the file generated I get something like this:

enter image description here

I'm lost since I don't seem to find anything about encoding in x86, so any help would be appreciated.

Upvotes: 0

Views: 193

Answers (1)

Shift_Left
Shift_Left

Reputation: 1243

Consider for a moment a file that has this content, viewed in hexedit let's say.

ADDR  00 01 02 03 04 05 06 07  08 09 0A 0B 0C 0D 0E 0F
------------------------------------------------------
 00   0A 20 20 20 20 20 20 20  20 20 20 F0 9D 90 98 F0
 10   9F 84 09 09 10 00 00 00  03 7C 2D 1A 0A 0A 0A 0A

Dumping to console with CAT tries display what it thinks is UTF-8 text.

enter image description here

However, if you look at the same thing using a text editor then; enter image description here or enter image description here So your data was written to file correctly, but the means by which you want to view it is not compatible with the result your expecting. I believe there are applications for Linux, that will display arrays of bytes/words/dwords or qwords and even structures, somewhat analogous to hexedit

Upvotes: 1

Related Questions