板凳——————————————————(枯藤 )MIX汇编 前导(2)

本文展示了如何使用gcc将C源码转换为汇编代码,并最终编译运行。通过示例代码解释了从创建test.c文件到运行hello世界程序的过程,涉及了gcc编译选项-S和-g,以及gdb调试工具的使用。
摘要由CSDN通过智能技术生成

wannian07@wannian07-PC:~$ gedit test.c

#include <stdio.h>

int main(int argc, char *argv[]){

    printf("Hello %s\n","World!");  
    return 0;  

}

wannian07@wannian07-PC:~$ gcc -S test.c
wannian07@wannian07-PC:~$ gcc test.s -o test
wannian07@wannian07-PC:~$ ./test
Hello World!

//test.s

.file	"test.c"
.text
.section	.rodata

.LC0:
.string “World”
.LC1:
.string “Hello %s\n”
.text
.globl main
.type main, @function
main:
.LFB0:
pushq %rbp
.LCFI0:
movq %rsp, %rbp
.LCFI1:
subq $16, %rsp
movl %edi, -4(%rbp)
movq %rsi, -16(%rbp)
movl $.LC0, %esi
movl $.LC1, %edi
movl $0, %eax
call printf
movl $0, %eax
leave
.LCFI2:
ret
.LFE0:
.size main, .-main
.section .eh_frame,“a”,@progbits
.Lframe1:
.long .LECIE1-.LSCIE1
.LSCIE1:
.long 0
.byte 0x3
.string “zR”
.uleb128 0x1
.sleb128 -8
.uleb128 0x10
.uleb128 0x1
.byte 0x3
.byte 0xc
.uleb128 0x7
.uleb128 0x8
.byte 0x90
.uleb128 0x1
.align 8
.LECIE1:
.LSFDE1:
.long .LEFDE1-.LASFDE1
.LASFDE1:
.long .LASFDE1-.Lframe1
.long .LFB0
.long .LFE0-.LFB0
.uleb128 0
.byte 0x4
.long .LCFI0-.LFB0
.byte 0xe
.uleb128 0x10
.byte 0x86
.uleb128 0x2
.byte 0x4
.long .LCFI1-.LCFI0
.byte 0xd
.uleb128 0x6
.byte 0x4
.long .LCFI2-.LCFI1
.byte 0xc
.uleb128 0x7
.uleb128 0x8
.align 8
.LEFDE1:
.ident “GCC: (GNU) 9.3.0”
.section .note.GNU-stack,"",@progbits

.section .data #Directives
output: #Labels
.ascii “Hello world!\n”
.section .text
.globl main
main: #Labels
movl $4, %eax #No.4 syscall : write
movl $1, %ebx #write to stdout
movl $output, %ecx #address of string
movl $14, %edx #length of string
int $0x80 #invoke the syscall
movl $1, %eax # No.1 syscall : exit
movl $0, %ebx # return code: 0(success)
int $0x80 #invoke the syscall

/*
wannian07@wannian07-PC:~$ gedit hello.s
wannian07@wannian07-PC:~$ gcc hello.s -o hello
wannian07@wannian07-PC:~$ ./hello
Hello world!
wannian07@wannian07-PC:~$ echo $?
0

*/

wannian07@wannian07-PC:~$ gcc -g hello.s -o hello
wannian07@wannian07-PC:~$ gdb -q hello
Reading symbols from hello…done.
(gdb) list
1 .section .data #Directives
2 output: #Labels
3 .ascii “Hello world!\n”
4 .section .text
5 .globl main
6 main: #Labels
7 movl $4, %eax #No.4 syscall : write
—Type to continue, or q to quit—
8 movl $1, %ebx #write to stdout
9 movl $output, %ecx #address of string
10 movl $14, %edx #length of string
(gdb) help
List of classes of commands:

aliases – Aliases of other commands
breakpoints – Making program stop at certain points
data – Examining data
files – Specifying and examining files
internals – Maintenance commands
obscure – Obscure features
running – Running the program
stack – Examining the stack
status – Status inquiries
support – Support facilities
tracepoints – Tracing of program execution without stopping the program
user-defined – User-defined commands

Type “help” followed by a class name for a list of commands in that class.
Type “help all” for the list of all commands.
Type “help” followed by command name for full documentation.
—Type to continue, or q to quit—
Type “apropos word” to search for commands related to “word”.
Command name abbreviations are allowed if unambiguous.
(gdb) help data
Examining data.
:
:
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/wannian07/hello

Breakpoint 1, main () at hello.s:7
7 movl $4, %eax #No.4 syscall : write
(gdb) disassemble /r
Dump of assembler code for function main:
=> 0x0000000000400452 <+0>: b8 04 00 00 00 mov $0x4,%eax
0x0000000000400457 <+5>: bb 01 00 00 00 mov $0x1,%ebx
0x000000000040045c <+10>: b9 28 10 60 00 mov $0x601028,%ecx
0x0000000000400461 <+15>: ba 0e 00 00 00 mov $0xe,%edx
0x0000000000400466 <+20>: cd 80 int $0x80
0x0000000000400468 <+22>: b8 01 00 00 00 mov $0x1,%eax
0x000000000040046d <+27>: bb 00 00 00 00 mov $0x0,%ebx
0x0000000000400472 <+32>: cd 80 int $0x80
0x0000000000400474 <+34>: 66 2e 0f 1f 84 00 00 00 00 00 nopw %cs:0x0(%rax,%rax,1)
0x000000000040047e <+44>: 66 90 xchg %ax,%ax
End of assembler dump.
(gdb) info register
rax 0x400452 4195410
rbx 0x0 0
rcx 0x0 0
rdx 0x7fffffffcbe8 140737488341992
rsi 0x7fffffffcbd8 140737488341976
rdi 0x1 1
rbp 0x400480 0x400480 <__libc_csu_init>
rsp 0x7fffffffcaf8 0x7fffffffcaf8
r8 0x4004f0 4195568
r9 0x7ffff7de8ba0 140737351945120
r10 0x8 8
r11 0x7ffff7ffb19c 140737354117532
r12 0x400380 4195200
—Type to continue, or q to quit—
r13 0x7fffffffcbd0 140737488341968
r14 0x0 0
r15 0x0 0
rip 0x400452 0x400452
eflags 0x246 [ PF ZF IF ]
cs 0x33 51
ss 0x2b 43
ds 0x0 0
es 0x0 0
fs 0x0 0
gs 0x0 0
(gdb) si
8 movl $1, %ebx #write to stdout
(gdb) disassemble /r
Dump of assembler code for function main:
0x0000000000400452 <+0>: b8 04 00 00 00 mov $0x4,%eax
=> 0x0000000000400457 <+5>: bb 01 00 00 00 mov $0x1,%ebx
0x000000000040045c <+10>: b9 28 10 60 00 mov $0x601028,%ecx
0x0000000000400461 <+15>: ba 0e 00 00 00 mov $0xe,%edx
0x0000000000400466 <+20>: cd 80 int $0x80
0x0000000000400468 <+22>: b8 01 00 00 00 mov $0x1,%eax
0x000000000040046d <+27>: bb 00 00 00 00 mov $0x0,%ebx
0x0000000000400472 <+32>: cd 80 int $0x80
0x0000000000400474 <+34>: 66 2e 0f 1f 84 00 00 00 00 00 nopw %cs:0x0(%rax,%rax,1)
0x000000000040047e <+44>: 66 90 xchg %ax,%ax
End of assembler dump.
(gdb) info register
rax 0x4 4
rbx 0x0 0
rcx 0x0 0
rdx 0x7fffffffcbe8 140737488341992
rsi 0x7fffffffcbd8 140737488341976
rdi 0x1 1
rbp 0x400480 0x400480 <__libc_csu_init>
rsp 0x7fffffffcaf8 0x7fffffffcaf8
r8 0x4004f0 4195568
r9 0x7ffff7de8ba0 140737351945120
r10 0x8 8
r11 0x7ffff7ffb19c 140737354117532
r12 0x400380 4195200
—Type to continue, or q to quit—
:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值