博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Linux 编译汇编

Posted on 2011-09-14 15:05  bug yang  阅读(2444)  评论(0编辑  收藏  举报
将下面的代码保存为asm.s文件:
 .section .data
data_items: #These are the data items
.
long 3,67,34,222,45,75,54,34,44,33,22,11,66,0
.section .text
.globl _start
_start:
movl $
0, %edi # move 0 into the index register
movl data_items(,
%edi,4), %eax # load the first byte of data
movl
%eax, %ebx # since this is the first item, %eax is
# the biggest
start_loop: # start loop
cmpl $
0, %eax # check to see if we've hit the end
je loop_exit
incl
%edi # load next value
movl data_items(,
%edi,4), %eax
cmpl
%ebx, %eax # compare values
jle start_loop # jump to loop beginning
if the new
# one isn
't bigger
movl %eax, %ebx # move the value as the largest
jmp start_loop # jump to loop beginning
loop_exit:
#
%ebx is the status code for the _exit system call
# and it already has the maximum number
movl $
1, %eax #1 is the _exit() syscall
int $0x80

在Linux终端中编译这段代码:

  $as asm.s -o asm.o   //编译这段汇编代码

  $ld asm.o -o asm     //链接编译代码为可执行程序

  $ ./asm                   //执行程序

如何在Linux查看自己写的程序的汇编代码?

有两种方法:

1、gcc -S temp.c 这样会生成temp.s的汇编文件

2、$ gcc temp.c -g

   $ objdump -dS a.out