Original link: Linux simple beauty (ii)
We saw in the previous chapter how to do simple things with syscall, and now we see if we can directly invoke the functions in the C standard library to do some "complex" things quickly:
1 Section . Data2FT DB"Now is %d",Ten3 4 Section . Text5 extern puts6 extern exit7 extern sleep8 extern printf9 Global MainTen One Main: A movEdi One - again: - DecEDI the PushEDI - Pushft - Pagerprintf - + Push 1 - PagerSleep + A CMPEdi0 at jnzagain - - Pushmsg - Pagerputs - - Push 0 in PagerExit - to msg: +Db"Happy xxx day!",0
The above code is simple, counting from 10 to 0, then printing one line and ending. Unlike the previous code, the function in the C standard library is called. The compilation is the same as before:
Nasm-e elf Main.asm
Let's see how to connect:
Gcc-m32-o Main MAIN.O
Good bird! It is important to note that my OS is ubuntu64, and the ASM code is 32 bits, if you start with ld-m elf_i386-lc-o main MAIN.O, you will first be prompted to find the C library, then you can enter/usr/lib, then use:
sudo ln-sv/lib/i386-linux-gun/libc.so.6 libc.so
Create a soft connect fix. But at run time prompt cannot find executable file! The file is clearly in the! Then connect with GCC, but to change the _start to main and load 32 libraries:
sudo apt-get install Ia32-libs
You will also be prompted not to find the H file, and then load the library:
sudo apt-get install G++-multilib
There are also 2 family libraries that can be loaded if necessary:
sudo apt-get install libc6:i386 libgcc1:i386 gcc-4.6-base:i386
Libstdc++5:i386 libstdc++6:i386
sudo apt-get install libc6-i386
Finally, some C code when compiling with STD=C99 will find that the hint can not get the structure size, then change to the following:
Gcc-d_gnu_source-std=c99 MAIN.C