jameshfisher jameshfisher - 25 days ago 10
Linux Question

Reading from a file in assembly

I'm trying to learn assembly -- x86 in a Linux environment. The most useful tutorial I can find is Writing A Useful Program With NASM. The task I'm setting myself is simple: read a file and write it to stdout.

This is what I have:

section .text ; declaring our .text segment
global _start ; telling where program execution should start

_start: ; this is where code starts getting exec'ed

; get the filename in ebx
pop ebx ; argc
pop ebx ; argv[0]
pop ebx ; the first real arg, a filename

; open the file
mov eax, 5 ; open(
mov ecx, 0 ; read-only mode
int 80h ; );

; read the file
mov eax, 3 ; read(
mov ebx, eax ; file_descriptor,
mov ecx, buf ; *buf,
mov edx, bufsize ; *bufsize
int 80h ; );

; write to STDOUT
mov eax, 4 ; write(
mov ebx, 1 ; STDOUT,
; mov ecx, buf ; *buf
int 80h ; );

; exit
mov eax, 1 ; exit(
mov ebx, 0 ; 0
int 80h ; );


A crucial problem here is that the tutorial never mentions how to create a buffer, the
bufsize
variable, or indeed variables at all.

How do I do this?

(An aside: after at least an hour of searching, I'm vaguely appalled at the low quality of resources for learning assembly. How on earth does any computer run when the only documentation is the hearsay traded on the 'net?)

Answer

you must declare your buffer in bss section and the bufsize in data

section .data
   bufsize dw      1024

section .bss
   buf     resb    1024
Comments