Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
assembler writing to file question
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
ichbinsisyphos
Guru
Guru


Joined: 08 Dec 2006
Posts: 547

PostPosted: Mon Dec 03, 2012 5:26 pm    Post subject: assembler writing to file question Reply with quote

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
View user's profile Send private message
erm67
Tux's lil' helper
Tux's lil' helper


Joined: 01 Nov 2005
Posts: 130
Location: somewhere in Berlusconia.

PostPosted: Mon Dec 03, 2012 6:27 pm    Post subject: Re: assembler writing to file question Reply with quote

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?
_________________
Truck!!
A posse ad esse non valet consequentia
Πάντα ῥεῖ
Back to top
View user's profile Send private message
ichbinsisyphos
Guru
Guru


Joined: 08 Dec 2006
Posts: 547

PostPosted: Mon Dec 03, 2012 6:44 pm    Post subject: Reply with quote

Hey, why not write it in C? It's an ugly language too :P
Back to top
View user's profile Send private message
erm67
Tux's lil' helper
Tux's lil' helper


Joined: 01 Nov 2005
Posts: 130
Location: somewhere in Berlusconia.

PostPosted: Mon Dec 03, 2012 7:13 pm    Post subject: Reply with quote

ichbinsisyphos wrote:
Hey, why not write it in C? It's an ugly language too :P


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 :-)
_________________
Truck!!
A posse ad esse non valet consequentia
Πάντα ῥεῖ
Back to top
View user's profile Send private message
Amaranatha
n00b
n00b


Joined: 30 Nov 2004
Posts: 48
Location: Spain, Europe

PostPosted: Mon Dec 03, 2012 7:22 pm    Post subject: Reply with quote

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
View user's profile Send private message
ichbinsisyphos
Guru
Guru


Joined: 08 Dec 2006
Posts: 547

PostPosted: Mon Dec 03, 2012 7:48 pm    Post subject: Reply with quote

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
View user's profile Send private message
Amaranatha
n00b
n00b


Joined: 30 Nov 2004
Posts: 48
Location: Spain, Europe

PostPosted: Mon Dec 03, 2012 10:21 pm    Post subject: Reply with quote

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
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 7742
Location: Somewhere over Atlanta, Georgia

PostPosted: Tue Dec 04, 2012 4:39 pm    Post subject: Reply with quote

Moved from Off the Wall to Portage & Programming.

- John
_________________
This space intentionally left blank.
Back to top
View user's profile Send private message
Hu
Watchman
Watchman


Joined: 06 Mar 2007
Posts: 8922

PostPosted: Tue Dec 04, 2012 11:53 pm    Post subject: Reply with quote

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
View user's profile Send private message
tomtom69
Tux's lil' helper
Tux's lil' helper


Joined: 09 Nov 2010
Posts: 104

PostPosted: Wed Dec 05, 2012 8:48 pm    Post subject: Reply with quote

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
View user's profile Send private message
ichbinsisyphos
Guru
Guru


Joined: 08 Dec 2006
Posts: 547

PostPosted: Wed Dec 05, 2012 9:51 pm    Post subject: Reply with quote

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 :lol: Creating and opening the file both need the filename.
"db" (1 byte numbers) work just fine, btw.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum