linux - Are there any examples of programs that generate text text files as output in NASM? -
i need make program outputs text file extension of .dna, don't know if can that, , if text file compatible need compare afterwards. anyway, i'm not sure how this. tried examples nasm, didn't find much. have idea of i'd need do, don't know call generate file.
afterwards i'd need write stuff it, i'm not sure on how go on that. point me examples or something? need see required write own thing.
here's example using system calls. basically, open file, write data it, close , exit:
; nasm -f elf file.asm ; ld -m elf_i386 file.o bits 32 section .data ; don't forget 0 terminator if akes c string! filename: db 'test.txt', 0 ; error message printed write(). function doesn't ; use c string no need 0 here, need length. error_message: db 'something went wrong.', 10 ; 10 == \n ; next line means current location minus error_message location ; works out message length. ; many of system calls use pointer+length pairs instead of ; 0 terminated strings. error_message_length: equ $ - error_message ; message we'll write our file, same error message hello: db 'hello, file!', 10 ; 10 newline @ end hello_length: equ $ - hello fd: dd 0 ; global int variable in c ; global variables bad idea , there's other ; ways it, simplicity i'm using 1 here ; other ways bit more work in asm section .text global _start _start: ; first, open or create file. in c be: ; // $ man 2 creat ; int fd = creat("file.txt", 0644); // second argument permission ; syscall numbers /usr/include/asm/unistd_32.h mov eax, 8 ; creat mov ebx, filename ; first argument mov ecx, 644o ; suffix o means octal in nasm, leading 0 in c. see: http://www.nasm.us/doc/nasmdoc3.html int 80h ; calls kernel cmp eax, -1 ; creat returns -1 on error je error mov [fd], eax ; return value in eax - file descriptor ; now, we'll write file ; // man 2 write ; write(fd, hello_pointer, hello_length) mov eax, 4 ; write mov ebx, [fd], mov ecx, hello mov edx, hello_length int 80h cmp eax, -1 ; should close file in normal program upon write error ; since open, meh, since terminate kernel ; clean after je error ; , close file ; // man 2 close ; close(fd); mov eax, 6 ; close mov ebx, [fd] int 80h ; , close program calling exit(0); mov eax, 1 ; exit mov ebx, 0 ; return value int 80h error: mov eax, 4 ; write mov ebx, 1 ; write stdout - file #1 mov ecx, error_message ; pointer string mov edx, error_message_length ; length of string int 80h ; print mov eax, 1 ; exit mov ebx, 1 ; return value int 80h
the file called a.out
if copied link command above. -o
option ld changes that.
we can call c functions, helps if need write out things numbers.
; nasm -f elf file.asm ; gcc -m32 file.o -nostdlib -lc # notice we're using gcc link, makes things bit easier ; # options are: -m32, 32 bit, -nostdlib, don't try use c lib cuz main() ; # , finally, -lc add of c standard library want bits 32 ; docs here: http://www.nasm.us/doc/nasmdoc6.html ; declare c functions external symbols. leading underscore c thing. extern fopen extern fprintf extern fclose section .data ; don't forget 0 terminator if akes c string! filename: db 'test.txt', 0 filemode: db 'wt', 0 ; mode fopen in c format_string: db 'hello number! %d it.', 10, 0 ; new line , 0 terminator ; error message printed write(). function doesn't ; use c string no need 0 here, need length. error_message: db 'something went wrong.', 10 ; 10 == \n ; next line means current location minus error_message location ; works out message length. ; many of system calls use pointer+length pairs instead of ; 0 terminated strings. error_message_length: equ $ - error_message fp: dd 0 ; global int variable in c ; global variables bad idea , there's other ; ways it, simplicity i'm using 1 here ; other ways bit more work in asm section .text global _start _start: ; first, open or create file. in c be: ; file* fp = fopen("text.txt", "wt"); ; arguments c functions pushed on stack, right left. push filemode ; "wt" push filename ; "text.txt" call fopen add esp, 8 ; need clean our own stack. since pushed 2 four-byte items, need pop 8 bytes off. alternatively, have called pop twice, single add instruction keeps our registers cleaner. ; return value in eax, store in our fp variable after checking errors ; in c: if(fp == null) goto error; cmp eax, 0 ; check null je error mov [fp], eax; ; call fprintf(fp, "format string %d", 55); ; 55 random number print mov eax, 55 push eax ; arguments pushed, right left. want 4 byte int equal 55, eax push format_string mov eax, [fp] ; again using eax intermediate store our 4 bytes push stack push eax call fprintf add esp, 12 ; 3 words time clean ; fclose(fp); mov eax, [fp] ; again using eax intermediate store our 4 bytes push stack push eax call fclose ; rest unchanged above example ; , close program calling exit(0); mov eax, 1 ; exit mov ebx, 0 ; return value int 80h error: mov eax, 4 ; write mov ebx, 1 ; write stdout - file #1 mov ecx, error_message ; pointer string mov edx, error_message_length ; length of string int 80h ; print mov eax, 1 ; exit mov ebx, 1 ; return value int 80h
there's lot more can done here, few techniques eliminate global variables, or better error checking, or writing c style main() in assembly. should started in writing out text file. tip: files same writing screen, need open/create them first!
btw don't mix system calls , c library functions @ same time. c library (fprintf
etc) buffers data, system calls don't. if mix them, data might end written file in surprising order.
the code similar, different in 64 bit.
finally, same pattern can used translate c code asm - c calling convention same different functions, , linux system call convention argument placement etc. follows consistent pattern too.
further reading: http://en.wikipedia.org/wiki/x86_calling_conventions#cdecl on c calling convention
http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html on linux system calls
what purpose of ebp in following code? answer wrote while ago local variables in asm - have hints 1 way rid of global , describes how c compile it. (the other way rid of global either keep fd/fp in register , push , pop onto stack when need free register else)
and man pages referenced in code each function. linux prompt, things man 2 write
or man 3 fprintf
see more. (system calls in manual section 2 , c functions in manual section 3).
Comments
Post a Comment