miércoles, 30 de octubre de 2013

Simple file loading and display in assembler.

This program let's you load a file and display it in the STDOUT

#nasm -f elf tiny.asm
#gcc -Wall -s -nostdlib tiny.o

It uses some linux calls to load the file. 

This is intended to be a small description on how the .text, .data and .bss sections work. The code is in the .text section of the ELF file format while the other sections are basically the data zones.

;tiny.asm
BITS 32
GLOBAL _start
SECTION .text
_start:
pop ebx ;argc
pop ebx ;argv[0]
pop ebx ;the file name
mov eax,5 ;the syscall number for open()
;the name is in ebx
mov ecx,0   ;O_RDONLY as in fcntl
int 0x80
test eax,eax  ;it is not NULL
jns file_function
mov ebx,eax ;any error goes to ebx
mov eax,1   ;exit syscall
int 0x80
file_function:
mov ebx,eax ;file descriptor
mov eax,3 ;sys read
mov ecx,buf ;*buf
mov edx,bufsize ;*bufsize
int 0x80
write:
mov edx,eax
mov eax,4
mov ebx,1
int 0x80
exit: ;exit(0)
mov eax,1
mov ebx,0
int 0x80
section .data
   bufsize dw      1024

section .bss
   buf     resb    1024

check this site http://leto.net/writing/nasm.php

No hay comentarios:

Publicar un comentario