Read from assembly file

I am trying to learn the x86 build on Linux. The most useful tutorial I can find is Writing a Useful Program with NASM . The task I'm setting up is simple: read the file and write it to standard output.

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 ; ); 

The most important problem here is that the tutorial never mentions how to create a buffer, a bufsize variable bufsize or variables in general.

How to do it?

(Aside: after at least an hour of searching, I am vaguely shocked by the low quality of resources for training in collecting. How, in fact, does any computer work when the only documentation is rumors that are traded on the network?)

+4
source share
3 answers

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

 section .data bufsize dw 1024 section .bss buf resb 1024 
+2
source

Oh, that will be fun.

Assembly language has no variables. This is a higher level language construct. In assembly language, if you want variables, you do them yourself. Uphill. Round trip. In the snow.

If you need a buffer, you will either have to use some area of โ€‹โ€‹your stack as a buffer (after calling the appropriate stack installation instructions), or use some area on the heap. If your heap is too small, you need to do a SYSCALL statement (another INT 80h) to ask the operating system for more (via sbrk).

Another alternative is to learn about the ELF format and create a global variable in the corresponding section (I think it is .data).

The end result of any of these methods is a memory location that you can use. But your real "variables", as you are used to, from the now wonderful world of C, are your registers. And there are very few of them.

Assembler can help you with useful macros. Read the assembler documentation; I do not remember them from the head.

Life at this level is tough.

+7
source

After the call to open, the file descriptor is in eax. You rightfully transfer eax to ebx, where a call to read will look for it. Unfortunately, at this point you have already overwritten it with 3, syscall for reading.

0
source

All Articles