Linux Kernel series-10. Operating System Development kernel HelloWorld, linuxhelloworld

Source: Internet
Author: User

Linux Kernel series-10. Operating System Development kernel HelloWorld, linuxhelloworld

A. Let's first try assembly programming in Linux. For details, refer to the code.

[Section. data]; data in strHellodb "Hello, world! ", 0 AhSTRLENequ $-strHello [section. text]; Code in this global _ start; we must export the _ start entry so that the linker can recognize _ start: movedx, STRLENmovecx, strHellomovebx, 1 moveax, 4; sys_writeint0x80; system Call movebx, 0 moveax, 1; sys_exitint0x80; System Call

Compilation Method:

Nasm-f elf hello. asm-o hello. o

Ld-m elf_i386-s-o hello. o

./Hello

The running result is to print Hello, world!

The default entry point is _ start. We must not only define it, but also use the global keyword to export it so that the linked program can find it. There is no need to go into the two system calls because we cannot use Linux system calls in our own OS.

B. Simultaneous use of assembly and C

; Compile the Link Method; (the '-S' option of ld indicates "strip all"); $ nasm-f elf foo. asm-o foo. o; $ gcc-c bar. c-o bar. o; $ ld-s hello. o bar. o-o foobar; $. /foobar; the 2nd one; $ extern choose; int choose (int a, int B); [section. data]; data in this num1stdd3num2nddd4 [section. text]; Code in this global _ start; we must export the _ start entry so that the linker can recognize global myprint; export this function to make bar. c Use _ start: pushdword [num2nd]; '. pushdword [num1st]; | callchoose; | choose (num1st, num2nd); addesp, 8;/movebx, 0 moveax, 1; sys_exitint0x80; System Call; void myprint (char * msg, int len) myprint: movedx, [esp + 8]; lenmovecx, [esp + 4]; msgmovebx, 1 moveax, 4; sys_writeint0x80; System Call ret

1. Because the function myprint () is used in bar. c, use the global keyword to export it.

2. Because the function choose () defined outside this file is used, the keyword extern should be used for declaration.

3. Both myprint () and choose () follow the C call Convention. The following parameters are first imported into the stack, and the caller clears the stack.

void myprint(char* msg, int len);int choose(int a, int b){if(a >= b){myprint("the 1st one\n", 13);}else{myprint("the 2nd one\n", 13);}return 0;}

Compilation and execution process:

Nasm-f elf-o foo. o foo. asm

Gcc-m32-c-o bar. o bar. c

Ld-m elf_i386-s-o foobar foo. o bar. o

./Foobar

Running result: the 2nd one

With the global and extern keywords, You can freely come and go between the compilation and C code.

 

 

Source code]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.