I'm a newbie for Linux, for assembly programming (GAS) and for English. So, sorry if I write something wrong. I hope Google Translate will help me enough to write it all not too bad.
I want to know how to write a set of numbers, stored in section .data
, into the text file without calling functions from any libraries (like printf()
) and do this by assembler-only possibilities. I don't want ready-made solution. I want to know what Linux kernel does after my write call. How it works? How it designed?
I'm trying to print list of numbers into STDOUT, but I receive an odd sign in my terminal. I thought this occurs because I don't understand how to work with write system call correctly.
This is what I got for now
.section .data
list: .long 12, 31, 42
.section .text
.globl _start
_start:
movl $4, %eax
movl $1, %ebx
movl $list, %ecx
movl $12, %edx
int $0x80
movl $1, %eax
movl $0, %ebx
int $0x80
This code works fine for .ascii "Hello world\0"
, but not for list of numbers.
First of all:
Even library functions (eg. from libc.so) internally use system calls.
A system call more or less is a special variant of a CALL instruction. This instruction will call some code (as CALL does) but it will place the CPU into privileged mode (in this mode the CPU has access to addresses it would not be able to access in user mode) and the address where the function to be called is located is fixed (defined by a special register which can only be accessed in privileged mode).
The function which is called evaluates the EAX register (assuming 32-bit Linux for x86) and does the desired action (if EAX is 4 then a "write" is done).
The code beyond the system call is very, very large and complex. In the end a system call will result in writing data to I/O ports and memory regions that for example belong to a disk drive or a graphics card.
To write characters to the screen for example data is written to the graphics memory which is located at the unmapped address 0xB8000 which corresponds to the mapped address 0xC00B8000 in older kernels.
This means a "mov" instruction writing some data to such an address will result in a character being displayed on the screen when the screen is in text mode.
However this address (0xC00B8000) can only be accessed when the CPU is in privileged mode. Otherwise an exception (this is also some kind of "CALL" which is caused by the hardware and not by the software) is thrown; the routine that is called in this case would print out something like "Bus error" and stop the program.
--- Edit ---
About your edited question:
The "write" system call will write ASCII characters to the screen or a file.
If you want to write numbers you'll have to convert the number to a sequence of ASCII characters.