日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C指针原理(21)-C指针基础-ATT汇编

發布時間:2025/3/12 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C指针原理(21)-C指针基础-ATT汇编 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

helloworld-高級版

.section .data#初始化的變量 output:.ascii "hello,world\n"#要打印的字符串,.data為初始化值的變量。output是標簽,指示字符串開始的位置,ascii為數據類型 .section .bss#未初始化的變量,由0填充的緩沖區.lcomm num,20#lcomm為本地內存區域,即本地匯編外的不能進行訪問。.comm是通用內存區域。 .section .text#匯編語言指令碼.globl _start#啟動入口_start:movl $4,%eax#調用的系統功能,4為write movl $output,%ecx#要打印的字符串movl $1,%ebx#文件描述符,屏幕為1 movl $12,%edx#字符串長度int $0x80#顯示字符串hello,worldmovl $0,%eaxmovl $num,%edimovl $65,1(%edi)#A 的asciimovl $66,2(%edi)#B 的ascii movl $67,3(%edi)#C 的ascii movl $68,4(%edi)#D 的asciimovl $10,5(%edi)#\n的ascii movl $4,%eax#調用的系統功能,4為write movl $num,%ecx#要打印的字符串 movl $1,%ebx#文件描述符,屏幕為1 movl $6,%edx#字符串長度int $0x80#顯示字符串ABCDmovl $1,%eax#1為退出movl $0,%ebx#返回給shell的退出代碼值int $0x80#內核軟中斷,退出系統

效果及調試, -gstabs用于生成調試信息

xxx-lx@xxx-lx-desktop:~/private/mytest$ as -gstabs -o hello.o hello.s

xxx-lx@xxx-lx-desktop:~/private/mytest$ ld -o hello hello.o

xxx-lx@xxx-lx-desktop:~/private/mytest$ ./hello

hello,world

ABCD

xxx-lx@xxx-lx-desktop:~/private/mytest$ gdb hello

GNU gdb (GDB) 7.1-ubuntu

Copyright ? 2010 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html;

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law. Type “show copying”

and “show warranty” for details.

This GDB was configured as “x86_64-linux-gnu”.

For bug reporting instructions, please see:

http://www.gnu.org/software/gdb/bugs/;…

Reading symbols from /home/xxx-lx/private/mytest/hello…done.

(gdb) list

1 .section .data#初始化的變量

2 output:

3 .ascii “hello,world\n”

4 #要打印的字符串,.data為初始化值的變量。output是標簽,指示字符串開始的位置,ascii為數據類型

5 .section .bss#未初始化的變量,由0填充的緩沖區

6 .lcomm num,20

7 #lcomm為本地內存區域,即本地匯編外的不能進行訪問。.comm是通用內存區域。

8 .section .text#匯編語言指令碼

9 .globl _start#啟動入口

10 _start:

(gdb) run

Starting program: /home/xxx-lx/private/mytest/hello

hello,world

ABCD

Program exited normally.

(gdb)

linux-匯編-調用C庫函數

1、使用GCC編譯

.section .data
output:
.asciz “http://xxx.iteye.com\n”
.section .text
.global main
main:
push $output
call printf
addl $4,%esp
push $0
call exit

gcc -o test test.s
./test
http://xxx.iteye.com
2、使用匯編器編譯,使用動態鏈接-dynamic-linker,要求后跟SO庫,可使用find / -name ld*.so來尋找鏈接庫,每個LINUX版本不一樣,鏈接庫不一樣,筆者用的是puppy linux,鏈接庫名為ld-linux.so.2

.section .data
output:
.asciz “http://xxx.iteye.com\n”
.section .text
.global _start
_start:
push $output
call printf
addl $4,%esp
push $0
call exit

as -o test.o test.s
ld -lc -dynamic-linker /lib/ld-linux.so.2 -o test test.o
./test
http://xxx.iteye.com

匯編中通用寄存器的目的

1、EAX和AX:累加器,所有的I/O指令用它來與外部設備傳送信息

2、EBX和BX:在計算存儲單元地址時常用作基地址寄存器

3、ECX和CX:保存計數值

4、EDX和DX:做四字或二字運算時,可以把EDX(DX)和EAX(AX)組合在一起存放一個四字或二字長的數據,在對某些I/O操作時,DX可以放I/O的端口地址

5、ESP和SP:堆棧棧頂指針。

6、EBP和BP:基址寄存器

7、ESI和SI:源變址

8、EDI和DI:目的變址

LINUX/UNIX一般 都安裝了binutils,如果沒有安裝,在UBUNTU下可以使用apt-get

1.xxx@xxx-laptop:~$ sudo apt-get install binutils

[sudo] password for xxx:

正在讀取軟件包列表… 完成

正在分析軟件包的依賴關系樹

正在讀取狀態信息… 完成

binutils 已經是最新的版本了。

下列軟件包是自動安裝的并且現在不需要了:

libany-moose-perl libtest-exception-perl libmouse-perl gcc-4.4-multilib

ibus-pinyin-db-open-phrase libclass-c3-perl libdata-optlist-perl

libclass-c3-xs-perl libparams-util-perl lib64gomp1 libmro-compat-perl

lib64gcc1 pinyin-database libsub-install-perl libc6-amd64 libc6-dev-amd64

gcc-multilib libclass-method-modifiers-perl libsub-uplevel-perl lib64stdc++6

libsub-exporter-perl libalgorithm-c3-perl

使用’apt-get autoremove’來刪除它們

升級了 0 個軟件包,新安裝了 0 個軟件包,要卸載 0 個軟件包,有 0 個軟件包未被升級。

2.也可以在其主頁上用源代碼安裝

http://www.gnu.org/software/binutils/

1、調試hello,要求編譯時指定了-gstabs選項

2、運行hello

xxx-lx@xxx-lx-desktop:~/private/mytest$ ./hello

hello,world

ABCD

3、用gdb打開hello

xxx-lx@xxx-lx-desktop:~/private/mytest$ gdb hello

GNU gdb (GDB) 7.1-ubuntu

Copyright ? 2010 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html;

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law. Type “show copying”

and “show warranty” for details.

This GDB was configured as “x86_64-linux-gnu”.

For bug reporting instructions, please see:

http://www.gnu.org/software/gdb/bugs/;…

Reading symbols from /home/xxx-lx/private/mytest/hello…done.

4、列出源代碼

(gdb) list

warning: Source file is more recent than executable.

1 .section .data#初始化的變量

2 output:

3 .ascii “hello,world\n”

4 #要打印的字符串,.data為初始化值的變量。output是標簽,指示字符串開始的位置,ascii為數據類型

5 .section .bss#未初始化的變量,由0填充的緩沖區

6 .lcomm num,20

7 #lcomm為本地內存區域,即本地匯編外的不能進行訪問。.comm是通用內存區域。

8 .section .text#匯編語言指令碼

9 .globl _start#啟動入口

10 _start:

(gdb) list

11 movl $4,%eax#調用的系統功能,4為write

12 movl $output,%ecx#要打印的字符串

13 movl $1,%ebx#文件描述符,屏幕為1

14 movl $12,%edx#字符串長度

15 int $0x80#顯示字符串hello,world

16

17 movl $0,%eax

18 movl $num,%edi

19 movl $65,1(%edi)#A 的ascii

20 movl $66,2(%edi)#B 的ascii

5、設置斷點

(gdb) break 17

Breakpoint 1 at 0x4000c6: file hello.s, line 17.

6、運行至斷點

(gdb) run

Starting program: /home/xxx-lx/private/mytest/hello

hello,world

Breakpoint 1, _start () at hello.s:17

7、運行下條語句

17 movl $0,%eax

(gdb) next

18 movl $num,%edi

8、顯示所有寄存器的值

(gdb) info registers

rax 0x0 0

rbx 0x1 1

rcx 0x60011c 6291740

rdx 0xc 12

rsi 0x0 0

rdi 0x0 0

rbp 0x0 0x0

rsp 0x7fffffffe2d0 0x7fffffffe2d0

r8 0x0 0

r9 0x0 0

r10 0x0 0

r11 0x0 0

r12 0x0 0

r13 0x0 0

r14 0x0 0

r15 0x0 0

rip 0x4000cb 0x4000cb <_start+27>

eflags 0x202 [ IF ]

cs 0x33 51

ss 0x2b 43

ds 0x0 0

es 0x0 0

fs 0x0 0

—Type to continue, or q to quit—

gs 0x0 0

(gdb) next

19 movl $65,1(%edi)#A 的ascii
9、按十六進制格式輸出edi寄存器的值。/x表示16進制,/d表示10進制,/t表示二進制

(gdb) print/x $rdi

$3 = 0x600128

10、顯示所有寄存器值

(gdb) info registers

rax 0x0 0

rbx 0x1 1

rcx 0x60011c 6291740

rdx 0xc 12

rsi 0x0 0

rdi 0x600128 6291752

rbp 0x0 0x0

rsp 0x7fffffffe2d0 0x7fffffffe2d0

r8 0x0 0

r9 0x0 0

r10 0x0 0

r11 0x0 0

r12 0x0 0

r13 0x0 0

r14 0x0 0

r15 0x0 0

rip 0x4000d0 0x4000d0 <_start+32>

eflags 0x202 [ IF ]

cs 0x33 51

ss 0x2b 43

ds 0x0 0

es 0x0 0

fs 0x0 0

—Type to continue, or q to quit—

gs 0x0 0

(gdb) next

20 movl $66,2(%edi)#B 的ascii
11、顯示某個內存位置的值,x/nyz,其中n為字段數,y為格式(c為字符,d為10進制,x為16進制),z為字段長度(b為字節,n為16位字,w為32位字)

(gdb) next

21 movl $67,3(%edi)#C 的ascii

(gdb) x/3cb &num

0x600128 : 0 '\000’65 ‘A’ 66 ‘B’

(gdb) next

22 movl $68,4(%edi)#D 的ascii

(gdb) next

23 movl $10,5(%edi)#\n的ascii

(gdb) next

25 movl $4,%eax#調用的系統功能,4為write

(gdb) x/4cb &num

0x600128 : 0 '\000’65 ‘A’ 66 'B’67 ‘C’

12、退出gdb

(gdb)quit

linux-匯編靜態符號
.equ 符號名 值

比如

.equ PI 3.1415

使用方式 為:

movl $PI,%eax

匯編數據類型

.ascii 文本字符串

.asciz 以空字符結尾的文本字符串

.byte 字節值

.double 雙精度符點數

.float 單精度符點數

.int 32位整數

.long 64位整數

.octa 16字節整數

.quad 8字節整數

.short 16位整數

.single 單精度整數與float相同

匯編傳送指令

傳送數據元素

movl:32位長度

movw:16位長度

movb:8位長度

比如:

movl 源,目標

匯編-多值內存位置訪問(1)

.section .datamyvalue:.byte 67,68,69,70,0mygs:.asciz "%s\n".section .text .globl mainmain:movl $myvalue,%ecxinc %ecx#本來應輸出CDEF,68代表Dpush %ecxpush $mygs call printfpush $0call exitxxx@xxx-laptop:~/private/mytest$ gcc -o test12 test12.s

xxx@xxx-laptop:~/private/mytest$ ./test12

DEF

xxx@xxx-laptop:~/private/mytest$

xxx@xxx-laptop:~/private/mytest$ gcc -o test12 test12.s

xxx@xxx-laptop:~/private/mytest$ ./test12

E

xxx@xxx-laptop:~/private/mytest$

.section .datamyvalue:.byte 67,68,69,70,0mygs:.asciz "%c\n".section .text .globl mainmain:#基地址(偏移地址[必須為寄存器],數據元素變址,數據元素長度[必須為寄存器],)#基地址+偏移地址+數據元素變址數據元素長度movl $2,%ecxmovl myvalue(,%ecx,1),%ebx #將myvalue的變址為2,長度為1的數據值移到ebx中push %ebxpush $mygs call printfpush $0call exit.section .datamyvalue:.byte 67,68,69,70,0mygs:.asciz "%c\n"mygss:.asciz "%s\n" .section .text .globl mainmain:#以下為傳數值#基地址(偏移地址[必須為寄存器],數據元素變址,數據元素長度[必須為寄存器],) #基地址+偏移地址+數據元素變址數據元素長度 movl $2,%ecx movl myvalue(,%ecx,1),%ebx #將myvalue的變址為2,長度為1的數據值移到ebx中 push %ebx push $mygs call printf #以下為傳地址 movl $2,%ecx movl $myvalue,%ebx#傳myvalue的地址,變量前加上$ push %ebx push $mygss call printf #以下為傳數值 movl $2,%ecx movl myvalue,%ebx#傳myvalue第一個內存位置的數值 push %ebx push $mygs call printf #以下為傳地址,mov %eax,(%ebx)表示把eax值復制到寄存器ebx所代表內存位置中,寄存器中存放著地址movl $71,%edxmovl $myvalue,%eax;movb %dl,2(%eax)#eax指向位置后的第2個字節,將69改為71movl $myvalue,%ebxpush %ebxpush $mygss call printf push $0call exit

xxx@xxx-laptop:~/private/mytest$ gcc -o test12 test12.s

test12.s: Assembler messages:test12.s:0: Warning: end of file not at end of a line; newline insertedxxx@xxx-laptop:~/private/mytest$ ./test12ECDEFCCDGFxxx@xxx-laptop:~/private/mytest$ 匯編的movl使用.section .datamyvalue:.int 67.section .text .globl mainmain:movl $myvalue,%ecxpush $myvaluecall printfpush $0call exitxxx@xxx-laptop:~/private/mytest$ gcc -o test12 test12.sxxx@xxx-laptop:~/private/mytest$ ./test12Cxxx@xxx-laptop:~/private/mytest$

總結

以上是生活随笔為你收集整理的C指针原理(21)-C指针基础-ATT汇编的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。