View previous topic :: View next topic |
Author |
Message |
ichbinsisyphos Guru
Joined: 08 Dec 2006 Posts: 547
|
Posted: Mon Dec 03, 2012 5:26 pm Post subject: assembler writing to file question |
|
|
So I wrote my first assembler programs today and there's one thing I wasn't prepared for ... just to output anything to the console you have to convert numbers to strings, and that's frankly to much effort for me right now.
What would I have to do to write binary representations of numbers into a file? I thought this might be straight forward, but the file always ends up empty |
|
Back to top |
|
|
erm67 l33t
Joined: 01 Nov 2005 Posts: 653 Location: EU
|
Posted: Mon Dec 03, 2012 6:27 pm Post subject: Re: assembler writing to file question |
|
|
ichbinsisyphos wrote: | So I wrote my first assembler programs today and there's one thing I wasn't prepared for ... just to output anything to the console you have to convert numbers to strings, and that's frankly to much effort for me right now.
What would I have to do to write binary representations of numbers into a file? I thought this might be straight forward, but the file always ends up empty |
What about calling some libc functions from your assembler? _________________ Ok boomer
True ignorance is not the absence of knowledge, but the refusal to acquire it.
Ab esse ad posse valet, a posse ad esse non valet consequentia
My fediverse account: @erm67@erm67.dynu.net |
|
Back to top |
|
|
ichbinsisyphos Guru
Joined: 08 Dec 2006 Posts: 547
|
Posted: Mon Dec 03, 2012 6:44 pm Post subject: |
|
|
Hey, why not write it in C? It's an ugly language too |
|
Back to top |
|
|
erm67 l33t
Joined: 01 Nov 2005 Posts: 653 Location: EU
|
Posted: Mon Dec 03, 2012 7:13 pm Post subject: |
|
|
ichbinsisyphos wrote: | Hey, why not write it in C? It's an ugly language too |
Well how do you write to the file?
Call directly the kernel?
Well, you know, the kernel is also written in C ........... so to avoid calling C code you should avoid the kernel as well _________________ Ok boomer
True ignorance is not the absence of knowledge, but the refusal to acquire it.
Ab esse ad posse valet, a posse ad esse non valet consequentia
My fediverse account: @erm67@erm67.dynu.net |
|
Back to top |
|
|
Amaranatha n00b
Joined: 30 Nov 2004 Posts: 48 Location: Europe
|
Posted: Mon Dec 03, 2012 7:22 pm Post subject: |
|
|
Did you remember closing the file? Have you tried using a debugger? _________________ "Freedom incurs responsibility; that is why so many men fear it." - George Bernard Shaw |
|
Back to top |
|
|
ichbinsisyphos Guru
Joined: 08 Dec 2006 Posts: 547
|
Posted: Mon Dec 03, 2012 7:48 pm Post subject: |
|
|
I didn't think of closing the file, but it doesn't change anything anyway. So in principle it should work to write a binary number to a file without any hassle?
Code: | section .data
summand1 db 1
; len equ $-summand1
ergebnis db 0
filename db "addtest.dat",0
section .text
global _start
_start:
mov eax, 8 ; create file
mov ebx, filename ; with name
mov ecx, 00644Q ; rw_rw_rw permissions
int 0x80
mov ebx, eax ; move file descriptor from eax to ebx
mov eax, 5 ; open file in ebx
mov ecx, 2 ; for writing
int 0x80 ; do eet!
mov ebx, eax
mov eax, 4 ; write (fd still in ebx)
mov ecx, 5 ; number 5
add ecx, [summand1] ; add content of variable summand1 to it (in place)
mov [ergebnis], ecx
mov edx, 1 ;len ; hmm?
int 0x80
mov eax, 6 ; close file
int 0x80
mov eax, 1 ; exit
; mov ebx, 0 ; and return 0
mov ebx, [ergebnis] ; return result instead for testing
int 0x80 |
I have the feeling I am still missing something fundamental. These things haven't been covered in the documentation I've read so far. |
|
Back to top |
|
|
Amaranatha n00b
Joined: 30 Nov 2004 Posts: 48 Location: Europe
|
Posted: Mon Dec 03, 2012 10:21 pm Post subject: |
|
|
I haven't tested it, but I believe you are mixing things up with the file write. The second argument to write() (in ECX) should be a pointer to the memory area, while you seem to be storing the value you want to write. Try with LEA or MOV DWORD PTR or whichever syntax your assembler uses.
(My assembly is rusty, so take with a grain of salt) _________________ "Freedom incurs responsibility; that is why so many men fear it." - George Bernard Shaw |
|
Back to top |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10589 Location: Somewhere over Atlanta, Georgia
|
Posted: Tue Dec 04, 2012 4:39 pm Post subject: |
|
|
Moved from Off the Wall to Portage & Programming.
- John _________________ I can confirm that I have received between 0 and 499 National Security Letters. |
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21624
|
Posted: Tue Dec 04, 2012 11:53 pm Post subject: |
|
|
You may find strace useful, since it will show you what syscalls the kernel receives and what values are returned by them. |
|
Back to top |
|
|
tomtom69 Apprentice
Joined: 09 Nov 2010 Posts: 245 Location: Bavaria
|
Posted: Wed Dec 05, 2012 8:48 pm Post subject: |
|
|
Hi,
As Amarantha said: ECX should be loaded with a memory pointer address, not with the value itself (mov cx, offset ergebnis or lea cx,[ergebnis]).
In addition:
- "summand" and "ergebnis" are defined by "db" (which means "one byte"). You actually access them with 32 bits, so they should be defined as 32 bit variables ("dd" whatever your assembler offers)
- if 32 bits should be written, EDX should be loaded with "4", because it defines the number of bytes to be written instead of the number of dwords.
I am somewhat reminded to my first MS-DOS asm programs ... long time ago
tom |
|
Back to top |
|
|
ichbinsisyphos Guru
Joined: 08 Dec 2006 Posts: 547
|
Posted: Wed Dec 05, 2012 9:51 pm Post subject: |
|
|
Yes, content of ecx should be a reference, this is true. About the length/size I wondered myself ... it has to be 4 bytes? Well at least the registers are 4 bytes large.
But there is some other problem still, somewhere during file opening. I tried to write a predefined string to file and it doesn't work either. To console it works fine, but not to a file.
edit, damn I am an idiot Creating and opening the file both need the filename.
"db" (1 byte numbers) work just fine, btw. |
|
Back to top |
|
|
|