懒人李冰

记录我的生活、学习

汇编语言实现hello World

接下来的工作可能要用到汇编语言。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
.data                   # section declaration
msg:
    .ascii  "Hello, world!\n"   # our dear string
    len = . - msg               # length of our dear string

.text                           # section declaration

                            # we must export the entry point to the ELF linker or
    .global _start          # loader. They conventionally recognize _start as their 
                            # entry point. Use ld -e foo to override the default.

_start:

# write our string to stdout
    
    movl $len, %edx        # third argument:message length
    movl $msg, %ecx        # second argument:pointer to message to write
    movl $1, %ebx          # first argument:file handle(stdout) 
    movl $4, %eax          # system call number (sys_write)
    int  $0x80             # call kernel

# and exit

    movl $0, %ebx           # first argument:exit code
    movl $1, %eax           # system call number (sys_exit)
    int  $0x80              # call kernel