Ϊʲôx86ϵͳҪʹÓÃ[bx+idata]µÄ·½Ê½À´·ÃÎÊÊý¾Ý
±¾Ìû×îºóÓÉ sarnaga ÓÚ 2014-10-6 00:41 ±à¼Ñ§Ï°Íõˬ»ã±àµÄÒÉ»ó¡£
µ¥¸öµÄ±äÁ¿ºÜÈÝÒ׶ÔÓ¦³É¼Ä´æÆ÷£¬µ«ÊǸ´ÔÓµÄÊý¾Ý½á¹¹£¨ÈçÊý×éºÍ½á¹¹£©ÓÃÓÐÏ޵ļĴæÆ÷ÎÞ·¨±íʾ¡£
ÄÇôֻÄÜ°ÑËüÃÇ·ÅÔÚ´æ´¢Æ÷Àï¡£
Òª·ÃÎÊ´æ´¢Æ÷£¬¾Í±ØÐëÌṩµØÖ·¡£
ÎÒµÄÒÉ»óÊÇ£¬ÎªÊ²Ã´intel²»°´ÕÕ¡°Õý³£¡±Ë¼Â·£¬»ùµØÖ·+Æ«ÒƵØÖ·¡£
»ùµØÖ·£ºÖ¸Ïò¸´ÔÓÊý¾ÝµÄÆðʼµØÖ·¡£
Æ«ÒƵØÖ·£ºÖ¸Ïò¸´ÔÓÊý¾ÝÖеĵ¥¸öÔªËØ¡£
mov ax, ds:
mov es:[bx},ax
add bx,2ÕâÑùµÄ·½Ê½×îÈÝÒ×Àí½â£¬ÊµÏÖÆðÀ´Ò²×î¼òµ¥¡£
µ«ÊÇintel²¢Ã»Óа´ÕÕÕâÖÖ·½Ê½£¬Ëü²ÉÓõÄÊǹ̶¨»ùµØÖ·£¬µ¥¶ÀʹÓÃÆ«ÒÆÀ´±íʾ¡£
Æ«ÒƵØÖ·£º¼´Ö¸Ïò¸´ÔÓÊý¾ÝµÄÆðʼµØÖ·£¬ÓÖÖ¸Ïòµ¥¸öÔªËØ¡£
mov ax, 0
mov 16,ax
add bx,2ÕâÖÖÍêȫʹÓÃÆ«ÒƵØÖ·°ü´òÌìÏ£¬¼´²»ÈÝÒ×Àí½â£¬ÓÃÆðÀ´Ò²²»·½±ã¡£
ÄÇô£¬ÎªÊ²Ã´intel»áÕâÑùÑ¡Ôñ£¿
Èç¹û˵¶Î¼Ä´æÆ÷²»¹»£¬½âÊͲ»Í¨£¬ÒòΪ¶Î¼Ä´æÆ÷¿ÉÒÔ·´¸´Ê¹Óá£
»¹ÊÇ˵£¬·´¸´ÉèÖöμĴæÆ÷£¬»áÓ°ÏìЧÂÊ£¿
8086/8088¹²º¬ÓÐ14¸ö16λ¼Ä´æÆ÷£¬ÕÆÎÕÕâЩ¼Ä´æÆ÷µÄÃû³Æ·ûºÅ¡¢³¤¶È¡¢º¬ÒåºÍÓ÷¨ÊÇѧϰָÁîϵͳµÄ»ù´¡£¬¶Ô¿¼ÉúÕÆÎÕÖ¸Áî±àд»ã±àÓïÑÔ³ÌÐòÊǷdz£ÖØÒªµÄ¡£8086/8088ÖеļĴæÆ÷´Ó¹¦ÄÜÉÏ¿É»®·Ö³É4Àà¡£Èçͼ2-2 Ëùʾ£º
1.Êý¾Ýλ±í´ï
1.1 idata Èç mov ax,1
1.2 ¼Ä´æÆ÷ Èç mov ax,bx
1.3 ¶ÎµØÖ·SAºÍÆ«ÒƵØÖ·EA Èç mov ax,
¶Î¼Ä´æÆ÷,ĬÈÏÔÚdsÖÐ:
mov ax,
¶ÎµØַĬÈÏÔÚssÖУº
mov ax,
2.Ñ°Ö··½Ê½£º
Ö±½ÓÑ°Ö·
EA=idata ; SA=ds
¼ä½ÓÑ°Ö·
(ds)ÖÐ
(ss)ÖÐ
Ïà¶ÔÑ°Ö·
ÓÃÓڽṹÌåÈç,Êý×é¶þάÊý×é
Èç .idata , idata ,
»ùÖ·±äÖ·Ñ°Ö·:
ÓÃÓÚ¶þάÊý×éÈ磺
Ïà¶Ô»ùÖ·±äÖ·Ñ°Ö·
ÓÃÓÚ±í¸ñ½á¹¹ÖÐÊý×éºÍ¶øδÊý×é:
.idata
idata
3.¶¨ÒåÊý¾Ý¶Î Óë divÖ¸Áî
//¶¨ÒåÊý¾Ý¶Î data segment dd 100001 //dd define double wordË«×Ö dw 100 //dw define word db 1 //db define byte data ends //¶¨ÒåÕ»¶Î stack segment dw 200 dup 0 //dupÏ൱ÓÚ dw 0,0,0,0¡¡¶¨Òå200¸ö×Ö½ÚµÄÊý¾Ý stack ends //div ³ý·¨Ö¸Áî /** ¹æÔò£º 1.³ýÊýÓÐ8λºÍ16λÁ½ÖÖ 2.±»³ýÊýĬÈÏ·ÅÔÚax »òdx ºÍ axÖÐ, 3.³ýÊýΪ8λ,al´æÉÌ,ah´æÓàÊýÈç¹û16λ,ax´æÉÌ,dx´æÓàÊý Àý×Ó£º 100001/100 100001´óÓÚ65535,ÐèÒªaxºÍdxÁªºÏ´æ·Å,100001=186A1H **/ mov dx,1 mov ax,86A1H mov bx,100 div bx /** ½á¹û: ax=03E8H=1000 dx=1 **/
4.±ê¼Ç¼Ä´æÆ÷
ÓÃÓÚ±ê¼ÇÕý¸ºµÈµÄ¼Ä´æÆ÷
5.CPUÖжÏ
CPUÓÃ8λÖжÏÀàÐÍÂëͨ¹ýÖжÏÏòÁ¿±í²éÕÒµ½Öն˴¦Àí³ÌÐòµÄÈë¿ÚµØÖ·¡£
ÖжÏÏòÁ¿±íÔÚÄÚ´æÖдæ·Å£¬µØַΪ0000£º0000µ½0000£º03FFµÄ1024¸öµ¥Ôª
6.Á·Ï°:
linux Ï»ã±à±à³Ì
as -o hello.o hello.s //AT & T Óï·¨±àÒë nasm -f elf hello.asm // inter Óï·¨µÄ±àÒë ld -o hello hello.o //Á¬½Ó
µ÷ÊÔhello:
as --gstabs -o hello.o hello.s ld -o hello hello.o gdb hello (gdb) break 12 //ÔÚline 12 break (gdb) i reg eax //ÊäÈë¼Ä´æÆ÷axµÄÊý¾Ý #hello.s .data # Êý¾Ý¶ÎÉùÃ÷ msg : .string "Hello, world!\\n" # ÒªÊä³öµÄ×Ö·û´® len = . - msg # ×Ö´®³¤¶È .text # ´úÂë¶ÎÉùÃ÷ .global _start # Ö¸¶¨Èë¿Úº¯Êý _start: # ÔÚÆÁÄ»ÉÏÏÔʾһ¸ö×Ö·û´® movl $len, %edx # ²ÎÊýÈý£º×Ö·û´®³¤¶È movl $msg, %ecx # ²ÎÊý¶þ£ºÒªÏÔʾµÄ×Ö·û´® movl $1, %ebx # ²ÎÊýÒ»£ºÎļþÃèÊö·û(stdout) movl $4, %eax # ϵͳµ÷ÓúÅ(sys_write) int $0x80 # µ÷ÓÃÄں˹¦ÄÜ # Í˳ö³ÌÐò movl $0,%ebx # ²ÎÊýÒ»£ºÍ˳ö´úÂë movl $1,%eax # ϵͳµ÷ÓúÅ(sys_exit) int $0x80 # µ÷ÓÃÄں˹¦ÄÜ linux Ï c hello world vi hello.c ÄÚÈÝ£º #include<stdio.h> int main() { printf("Hello World\n"); //prints "Hello World" return 0; } gcc hello.c -o hello.c.o ./hello.c.o Êä³öHello World
È«¹úǶÈëʽÈ˲ÅÅàѵ»ùµØ
3. µÚ¶þ¸ö»ã±à³ÌÐò µÚ 18 Õ x86»ã±à³ÌÐò»ù´¡
ÉÏÒ»Ò³ ÏÂÒ»Ò³
3. µÚ¶þ¸ö»ã±à³ÌÐò ÇëµãÆÀ
Àý 18.2. ÇóÒ»×éÊýµÄ×î´óÖµµÄ»ã±à³ÌÐò
#PURPOSE: This program finds the maximum number of a # set of data items. # #VARIABLES: The registers have the following uses: # # %edi - Holds the index of the data item being examined # %ebx - Largest data item found # %eax - Current data item # # The following memory locations are used: # # data_items - contains the item data. A 0 is used # to terminate the data # .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
»ã±à¡¢Á´½Ó¡¢ÔËÐУº
$ as max.s -o max.o $ ld max.o -o max $ ./max $ echo $?
Õâ¸ö³ÌÐòÔÚÒ»×éÊýÖÐÕÒµ½Ò»¸ö×î´óµÄÊý£¬²¢°ÑËü×÷Ϊ³ÌÐòµÄÍ˳ö״̬¡£Õâ×éÊýÔÚ.data¶Î¸ø³ö£º
data_items: .long 3,67,34,222,45,75,54,34,44,33,22,11,66,0
.longָʾÉùÃ÷Ò»×éÊý£¬Ã¿¸öÊýÕ¼32룬Ï൱ÓÚCÓïÑÔÖеÄÊý×é¡£Õâ¸öÊý×鿪ͷ¶¨ÒåÁËÒ»¸ö·ûºÅdata_items£¬»ã±àÆ÷»á°ÑÊý×éµÄÊ×µØÖ·×÷Ϊdata_items·ûºÅËù´ú±íµÄµØÖ·£¬data_itemsÀàËÆÓÚCÓïÑÔÖеÄÊý×éÃû¡£data_itemsÕâ¸ö±êºÅûÓÐÓÃ.globlÉùÃ÷£¬ÒòΪËüÖ»ÔÚÕâ¸ö»ã±à³ÌÐòÄÚ²¿Ê¹Óã¬Á´½ÓÆ÷²»ÐèÒªÓõ½Õâ¸öÃû×Ö¡£³ýÁË.longÖ®Í⣬³£ÓõÄÊý¾ÝÉùÃ÷»¹ÓУº
.byte£¬Ò²ÊÇÉùÃ÷Ò»×éÊý£¬Ã¿¸öÊýÕ¼8λ
.ascii£¬ÀýÈç.ascii "Hello world"£¬ÉùÃ÷11¸öÊý£¬È¡ÖµÎªÏàÓ¦×Ö·ûµÄASCIIÂë¡£×¢Ò⣬ºÍCÓïÑÔ²»Í¬£¬ÕâÑùÉùÃ÷µÄ×Ö·û´®Ä©Î²ÊÇûÓÐ'\0'×Ö·ûµÄ£¬Èç¹ûÐèÒªÒÔ'\0'½áβ¿ÉÒÔÉùÃ÷Ϊ.ascii "Hello world\0"¡£
data_itemsÊý×éµÄ×îºóÒ»¸öÊýÊÇ0£¬ÎÒÃÇÔÚÒ»¸öÑ»·ÖÐÒÀ´Î±È½Ïÿ¸öÊý£¬Åöµ½0µÄʱºòÈÃÑ»·ÖÕÖ¹¡£ÔÚÕâ¸öÑ»·ÖУº
edi¼Ä´æÆ÷±£´æÊý×éÖеĵ±Ç°Î»Öã¬Ã¿´Î±È½ÏÍêÒ»¸öÊý¾Í°ÑediµÄÖµ¼Ó1£¬Ö¸ÏòÊý×éÖеÄÏÂÒ»¸öÊý¡£
ebx¼Ä´æÆ÷±£´æµ½Ä¿Ç°ÎªÖ¹ÕÒµ½µÄ×î´óÖµ£¬Èç¹û·¢ÏÖÓиü´óµÄÊý¾Í¸üÐÂebxµÄÖµ¡£
eax¼Ä´æÆ÷±£´æµ±Ç°Òª±È½ÏµÄÊý£¬Ã¿´Î¸üÐÂediÖ®ºó£¬¾Í°ÑÏÂÒ»¸öÊý¶Áµ½eaxÖС£
_start: movl $0, %edi
³õʼ»¯edi£¬Ö¸ÏòÊý×éµÄµÚ0¸öÔªËØ¡£
movl data_items(,%edi,4), %eax
ÕâÌõÖ¸Áî°ÑÊý×éµÄµÚ0¸öÔªËØ´«Ë͵½eax¼Ä´æÆ÷ÖС£data_itemsÊÇÊý×éµÄÊ×µØÖ·£¬ediµÄÖµÊÇÊý×éµÄϱ꣬4±íʾÊý×éµÄÿ¸öÔªËØÕ¼4×Ö½Ú£¬ÄÇôÊý×éÖеÚedi¸öÔªËصĵØÖ·Ó¦¸ÃÊÇdata_items + edi * 4£¬Ð´ÔÚÖ¸ÁîÖоÍÊÇdata_items(,%edi,4)£¬ÕâÖÖµØÖ·±íʾ·½Ê½ÔÚÏÂÒ»½Ú»¹»áÏêϸ½âÊÍ¡£
movl %eax, %ebx
ebxµÄ³õʼֵҲÊÇÊý×éµÄµÚ0¸öÔªËØ¡£ÏÂÃæÎÒÃǽøÈëÒ»¸öÑ»·£¬Ñ»·µÄ¿ªÍ·¶¨ÒåÒ»¸ö·ûºÅstart_loop£¬Ñ»·µÄĩβ֮ºó¶¨ÒåÒ»¸ö·ûºÅloop_exit¡£
start_loop: cmpl $0, %eax je loop_exit
±È½ÏeaxµÄÖµÊDz»ÊÇ0£¬Èç¹ûÊÇ0¾Í˵Ã÷µ½´ïÊý×éĩβÁË£¬¾ÍÒªÌø³öÑ»·¡£cmplÖ¸ÁÁ½¸ö²Ù×÷ÊýÏà¼õ£¬µ«¼ÆËã½á¹û²¢²»±£´æ£¬Ö»ÊǸù¾Ý¼ÆËã½á¹û¸Ä±äeflags¼Ä´æÆ÷Öеıê־λ¡£Èç¹ûÁ½¸ö²Ù×÷ÊýÏàµÈ£¬Ôò¼ÆËã½á¹ûΪ0£¬eflagsÖеÄZFλÖÃ1¡£jeÊÇÒ»¸öÌõ¼þÌøתָÁËü¼ì²éeflagsÖеÄZF룬ZFλΪ1Ôò·¢ÉúÌøת£¬ZFλΪ0Ôò²»Ìøת£¬¼ÌÐøÖ´ÐÐÏÂÒ»ÌõÖ¸Áî¡£¿É¼û±È½ÏÖ¸ÁîºÍÌõ¼þÌøתָÁîÊÇÅäºÏʹÓõģ¬Ç°Õ߸ıä±ê־룬ºóÕ߸ù¾Ý±ê־λ¾ö¶¨ÊÇ·ñÌøת¡£je¿ÉÒÔÀí½â³É¡°jump if equal¡±£¬Èç¹û²ÎÓë±È½ÏµÄÁ½ÊýÏàµÈÔòÌøת¡£
incl %edi movl data_items(,%edi,4), %eax
½«ediµÄÖµ¼Ó1£¬°ÑÊý×éÖеÄÏÂÒ»¸öÊý´«Ë͵½eax¼Ä´æÆ÷ÖС£
cmpl %ebx, %eax jle start_loop
°Ñµ±Ç°Êý×éÔªËØeaxºÍĿǰΪֹÕÒµ½µÄ×î´óÖµebx×ö±È½Ï£¬Èç¹ûÇ°ÕßСÓÚµÈÓÚºóÕߣ¬Ôò×î´óֵûÓб䣬Ìøתµ½Ñ»·¿ªÍ·±È½ÏÏÂÒ»¸öÊý£¬·ñÔò¼ÌÐøÖ´ÐÐÏÂÒ»ÌõÖ¸Áî¡£jle±íʾ¡°jump if less than or equal¡±¡£
movl %eax, %ebx jmp start_loop
¸üÐÂÁË×î´óÖµebxÈ»ºóÌøתµ½Ñ»·¿ªÍ·±È½ÏÏÂÒ»¸öÊý¡£jmpÊÇÒ»¸öÎÞÌõ¼þÌøתָÁʲôÌõ¼þÒ²²»Åжϣ¬Ö±½ÓÌøת¡£loop_exit·ûºÅºóÃæµÄÖ¸Áîµ÷_exitϵͳµ÷ÓÃÍ˳ö³ÌÐò¡£
ÉÏÒ»Ò³ ÉÏÒ»¼¶ ÏÂÒ»Ò³
2. x86µÄ¼Ä´æÆ÷ Æðʼҳ 4. Ñ°Ö··½Ê½
È«¹úǶÈëʽÈ˲ÅÅàѵ»ùµØ
4. Ñ°Ö··½Ê½ ÇëµãÆÀ
ͨ¹ýÉÏÒ»½ÚµÄÀý×ÓÎÒÃÇÁ˽⵽£¬·ÃÎÊÄÚ´æʱÔÚÖ¸ÁîÖпÉÒÔÓöàÖÖ·½Ê½±íʾÄÚ´æµØÖ·£¬±ÈÈç¿ÉÒÔÓÃÊý×é»ùµØÖ·¡¢ÔªËس¤¶ÈºÍϱêÈý¸öÁ¿À´±íʾ£¬Ôö¼ÓÁËÑ°Ö·µÄÁé»îÐÔ¡£±¾½Ú½éÉÜx86³£Óõļ¸ÖÖÑ°Ö··½Ê½£¨Addressing Mode£©¡£ÄÚ´æÑ°Ö·ÔÚÖ¸ÁîÖпÉÒÔ±íʾ³ÉÈçϵÄͨÓøñʽ£º
ADDRESS_OR_OFFSET(%BASE_OR_OFFSET,%INDEX,MULTIPLIER)
ËüËù±íʾµÄµØÖ·¿ÉÒÔÕâÑù¼ÆËã³öÀ´£º
FINAL ADDRESS = ADDRESS_OR_OFFSET + BASE_OR_OFFSET + MULTIPLIER * INDEX
ÆäÖÐADDRESS_OR_OFFSETºÍMULTIPLIER±ØÐëÊdz£Êý£¬BASE_OR_OFFSETºÍINDEX±ØÐëÊǼĴæÆ÷¡£ÔÚÓÐЩѰַ·½Ê½ÖлáÊ¡ÂÔÕâ4ÏîÖеÄijЩÏÏ൱ÓÚÕâЩÏîÊÇ0¡£
Ö±½ÓÑ°Ö·£¨Direct Addressing Mode£©¡£Ö»Ê¹ÓÃADDRESS_OR_OFFSETÑ°Ö·£¬ÀýÈçmovl ADDRESS, %eax°ÑADDRESSµØÖ·´¦µÄ32λÊý´«Ë͵½eax¼Ä´æÆ÷¡£
±äÖ·Ñ°Ö·£¨Indexed Addressing Mode£© ¡£ÉÏÒ»½ÚµÄmovl data_items(,%edi,4), %eax¾ÍÊôÓÚÕâÖÖÑ°Ö··½Ê½£¬ÓÃÓÚ·ÃÎÊÊý×éÔªËرȽϷ½±ã¡£
¼ä½ÓÑ°Ö·£¨Indirect Addressing Mode£©¡£Ö»Ê¹ÓÃBASE_OR_OFFSETÑ°Ö·£¬ÀýÈçmovl (%eax), %ebx£¬°Ñeax¼Ä´æÆ÷µÄÖµ¿´×÷µØÖ·£¬°ÑÄÚ´æÖÐÕâ¸öµØÖ·´¦µÄ32λÊý´«Ë͵½ebx¼Ä´æÆ÷¡£×¢ÒâºÍmovl %eax, %ebxÇø·Ö¿ª¡£
»ùÖ·Ñ°Ö·£¨Base Pointer Addressing Mode£©¡£Ö»Ê¹ÓÃADDRESS_OR_OFFSETºÍBASE_OR_OFFSETÑ°Ö·£¬ÀýÈçmovl 4(%eax), %ebx£¬ÓÃÓÚ·ÃÎʽṹÌå³ÉÔ±±È½Ï·½±ã£¬ÀýÈçÒ»¸ö½á¹¹ÌåµÄ»ùµØÖ·±£´æÔÚeax¼Ä´æÆ÷ÖУ¬ÆäÖÐÒ»¸ö³ÉÔ±ÔڽṹÌåÄÚµÄÆ«ÒÆÁ¿ÊÇ4×Ö½Ú£¬Òª°ÑÕâ¸ö³ÉÔ±¶ÁÉÏÀ´¾Í¿ÉÒÔÓÃÕâÌõÖ¸Áî¡£
Á¢¼´ÊýÑ°Ö·£¨Immediate Mode£©¡£¾ÍÊÇÖ¸ÁîÖÐÓÐÒ»¸ö²Ù×÷ÊýÊÇÁ¢¼´Êý£¬ÀýÈçmovl $12, %eaxÖеÄ$12£¬ÕâÆäʵ¸úѰַûʲô¹Øϵ£¬µ«Ò²Ëã×÷Ò»ÖÖÑ°Ö··½Ê½¡£
¼Ä´æÆ÷Ñ°Ö·£¨Register Addressing Mode£©¡£¾ÍÊÇÖ¸ÁîÖÐÓÐÒ»¸ö²Ù×÷ÊýÊǼĴæÆ÷£¬ÀýÈçmovl $12, %eaxÖеÄ%eax£¬Õâ¸úÄÚ´æѰַûʲô¹Øϵ£¬µ«Ò²Ëã×÷Ò»ÖÖÑ°Ö··½Ê½¡£ÔÚ»ã±à³ÌÐòÖмĴæÆ÷ÓÃÖú¼Ç·ûÀ´±íʾ£¬ÔÚ»úÆ÷Ö¸ÁîÖÐÔòÒªÓü¸¸öBit±íʾ¼Ä´æÆ÷µÄ±àºÅ£¬Õ⼸¸öBitÒ²¿ÉÒÔ¿´×÷¼Ä´æÆ÷µÄµØÖ·£¬µ«ÊǺÍÄÚ´æµØÖ·²»ÔÚÒ»¸öµØÖ·¿Õ¼ä¡£
È«¹úǶÈëʽÈ˲ÅÅàѵ»ùµØ
1. ×î¼òµ¥µÄ»ã±à³ÌÐò µÚ 18 Õ x86»ã±à³ÌÐò»ù´¡
ÉÏÒ»Ò³ ÏÂÒ»Ò³
1. ×î¼òµ¥µÄ»ã±à³ÌÐò ÇëµãÆÀ
Àý 18.1. ×î¼òµ¥µÄ»ã±à³ÌÐò
#PURPOSE: Simple program that exits and returns a # status code back to the Linux kernel # #INPUT: none # #OUTPUT: returns a status code. This can be viewed # by typing # # echo $? # # after running the program # #VARIABLES: # %eax holds the system call number # %ebx holds the return status # .section .data .section .text .globl _start _start: movl $1, %eax # this is the linux kernel command # number (system call) for exiting # a program movl $4, %ebx # this is the status number we will # return to the operating system. # Change this around and it will # return different things to # echo $? int $0x80 # this wakes up the kernel to run # the exit command
°ÑÕâ¸ö³ÌÐò±£´æ³ÉÎļþhello.s£¨»ã±à³ÌÐòͨ³£ÒÔ.s×÷ΪÎļþÃûºó׺£©£¬Óûã±àÆ÷£¨Assembler£©as°Ñ»ã±à³ÌÐòÖеÄÖú¼Ç·û·Òë³É»úÆ÷Ö¸ÁÉú³ÉÄ¿±êÎļþhello.o£º
$ as hello.s -o hello.o
È»ºóÓÃÁ´½ÓÆ÷£¨Linker£¬»òLink Editor£©ld°ÑÄ¿±êÎļþhello.oÁ´½Ó³É¿ÉÖ´ÐÐÎļþhello£º
$ ld hello.o -o hello
ΪʲôÓûã±àÆ÷·Òë³É»úÆ÷Ö¸ÁîÁË»¹²»ÐУ¬»¹ÒªÓÐÒ»¸öÁ´½ÓµÄ²½ÖèÄØ£¿Á´½ÓÖ÷ÒªÓÐÁ½¸ö×÷Óã¬Ò»ÊÇÐÞ¸ÄÄ¿±êÎļþÖеÄÐÅÏ¢£¬¶ÔµØÖ·×öÖض¨Î»£¬ÔÚµÚ 5.2 ½Ú ¡°¿ÉÖ´ÐÐÎļþ¡±Ïêϸ½âÊÍ£¬¶þÊǰѶà¸öÄ¿±êÎļþºÏ²¢³ÉÒ»¸ö¿ÉÖ´ÐÐÎļþ£¬ÔÚµÚ 2 ½Ú ¡°mainº¯ÊýºÍÆô¶¯Àý³Ì¡±Ïêϸ½âÊÍ¡£ÎÒÃÇÕâ¸öÀý×ÓËäȻֻÓÐÒ»¸öÄ¿±êÎļþ£¬µ«Ò²ÐèÒª¾¹ýÁ´½Ó²ÅÄܳÉΪ¿ÉÖ´ÐÐÎļþ¡£
ÏÖÔÚÖ´ÐÐÕâ¸ö³ÌÐò£¬ËüÖ»×öÁËÒ»¼þʾÍÊÇÍ˳ö£¬Í˳ö״̬ÊÇ4£¬µÚ 2 ½Ú ¡°×Ô¶¨Ò庯Êý¡±½²¹ýÔÚShellÖпÉÒÔÓÃÌØÊâ±äÁ¿$?µÃµ½ÉÏÒ»ÌõÃüÁîµÄÍ˳ö״̬£º
$ ./hello $ echo $? 4
ËùÒÔÕâ¶Î»ã±à´úÂëÏ൱ÓÚÔÚC³ÌÐòµÄmainº¯ÊýÖÐreturn 4;¡£ÎªÊ²Ã´»áÏ൱ÄØ£¿ÎÒÃÇÔÚµÚ 2 ½Ú ¡°mainº¯ÊýºÍÆô¶¯Àý³Ì¡±Ïêϸ½âÊÍ¡£
ÏÂÃæÖðÐзÖÎöÕâ¸ö»ã±à³ÌÐò¡£Ê×ÏÈ£¬#ºÅ±íʾµ¥ÐÐ×¢ÊÍ£¬ÀàËÆÓÚCÓïÑÔµÄ//×¢ÊÍ¡£
.section .data
»ã±à³ÌÐòÖÐÒÔ.¿ªÍ·µÄÃû³Æ²¢²»ÊÇÖ¸ÁîµÄÖú¼Ç·û£¬²»»á±»·Òë³É»úÆ÷Ö¸Á¶øÊǸø»ã±àÆ÷һЩÌØÊâָʾ£¬³ÆΪ»ã±àָʾ£¨Assembler Directive£©»òα²Ù×÷£¨Pseudo-operation£©£¬ÓÉÓÚËü²»ÊÇÕæÕýµÄÖ¸ÁîËùÒÔ¼Ó¸ö¡°Î±¡±×Ö¡£.sectionָʾ°Ñ´úÂë»®·Ö³ÉÈô¸É¸ö¶Î£¨Section£©£¬³ÌÐò±»²Ù×÷ϵͳ¼ÓÔØÖ´ÐÐʱ£¬Ã¿¸ö¶Î±»¼ÓÔص½²»Í¬µÄµØÖ·£¬²Ù×÷ϵͳ¶Ô²»Í¬µÄÒ³ÃæÉèÖò»Í¬µÄ¶Á¡¢Ð´¡¢Ö´ÐÐȨÏÞ¡£.data¶Î±£´æ³ÌÐòµÄÊý¾Ý£¬ÊǿɶÁ¿ÉдµÄ£¬Ï൱ÓÚC³ÌÐòµÄÈ«¾Ö±äÁ¿¡£±¾³ÌÐòÖÐûÓж¨ÒåÊý¾Ý£¬ËùÒÔ.data¶ÎÊǿյġ£
.section .text
.text¶Î±£´æ´úÂ룬ÊÇÖ»¶ÁºÍ¿ÉÖ´Ðеģ¬ºóÃæÄÇЩָÁÊôÓÚ.text¶Î¡£
.globl _start
_startÊÇÒ»¸ö·ûºÅ£¨Symbol£©£¬·ûºÅÔÚ»ã±à³ÌÐòÖдú±íÒ»¸öµØÖ·£¬¿ÉÒÔÓÃÔÚÖ¸ÁîÖУ¬»ã±à³ÌÐò¾¹ý»ã±àÆ÷µÄ´¦ÀíÖ®ºó£¬ËùÓеķûºÅ¶¼±»Ìæ»»³ÉËüËù´ú±íµÄµØÖ·Öµ¡£ÔÚCÓïÑÔÖÐÎÒÃÇͨ¹ý±äÁ¿Ãû·ÃÎÊÒ»¸ö±äÁ¿£¬Æäʵ¾ÍÊǶÁдij¸öµØÖ·µÄÄÚ´æµ¥Ôª£¬ÎÒÃÇͨ¹ýº¯ÊýÃûµ÷ÓÃÒ»¸öº¯Êý£¬Æäʵ¾ÍÊÇÌøתµ½¸Ãº¯ÊýµÚÒ»ÌõÖ¸ÁîËùÔڵĵØÖ·£¬ËùÒÔ±äÁ¿ÃûºÍº¯ÊýÃû¶¼ÊÇ·ûºÅ£¬±¾ÖÊÉÏÊÇ´ú±íÄÚ´æµØÖ·µÄ¡£
.globlָʾ¸æËß»ã±àÆ÷£¬_startÕâ¸ö·ûºÅÒª±»Á´½ÓÆ÷Óõ½£¬ËùÒÔÒªÔÚÄ¿±êÎļþµÄ·ûºÅ±íÖбê¼ÇËüÊÇÒ»¸öÈ«¾Ö·ûºÅ£¨ÔÚµÚ 5.1 ½Ú ¡°Ä¿±êÎļþ¡±Ïêϸ½âÊÍ£©¡£_start¾ÍÏñC³ÌÐòµÄmainº¯ÊýÒ»ÑùÌØÊ⣬ÊÇÕû¸ö³ÌÐòµÄÈë¿Ú£¬Á´½ÓÆ÷ÔÚÁ´½Óʱ»á²éÕÒÄ¿±êÎļþÖеÄ_start·ûºÅ´ú±íµÄµØÖ·£¬°ÑËüÉèÖÃΪÕû¸ö³ÌÐòµÄÈë¿ÚµØÖ·£¬ËùÒÔÿ¸ö»ã±à³ÌÐò¶¼ÒªÌṩһ¸ö_start·ûºÅ²¢ÇÒÓÃ.globlÉùÃ÷¡£Èç¹ûÒ»¸ö·ûºÅûÓÐÓÃ.globlÉùÃ÷£¬¾Í±íʾÕâ¸ö·ûºÅ²»»á±»Á´½ÓÆ÷Óõ½¡£
_start:
ÕâÀﶨÒåÁË_start·ûºÅ£¬»ã±àÆ÷ÔÚ·Òë»ã±à³ÌÐòʱ»á¼ÆËãÿ¸öÊý¾Ý¶ÔÏóºÍÿÌõÖ¸ÁîµÄµØÖ·£¬µ±¿´µ½ÕâÑùÒ»¸ö·ûºÅ¶¨Òåʱ£¬¾Í°ÑËüºóÃæÒ»ÌõÖ¸ÁîµÄµØÖ·×÷ΪÕâ¸ö·ûºÅËù´ú±íµÄµØÖ·¡£¶ø_startÕâ¸ö·ûºÅÓֱȽÏÌØÊ⣬ËüËù´ú±íµÄµØÖ·ÊÇÕû¸ö³ÌÐòµÄÈë¿ÚµØÖ·£¬ËùÒÔÏÂÒ»ÌõÖ¸Áîmovl $1, %eax¾Í³ÉÁ˳ÌÐòÖеÚÒ»Ìõ±»Ö´ÐеÄÖ¸Áî¡£
movl $1, %eax
ÕâÊÇÒ»ÌõÊý¾Ý´«ËÍÖ¸ÁÕâÌõÖ¸ÁîÒªÇóCPUÄÚ²¿²úÉúÒ»¸öÊý×Ö1²¢±£´æµ½eax¼Ä´æÆ÷ÖС£movµÄºó׺l±íʾlong£¬ËµÃ÷ÊÇ32λµÄ´«ËÍÖ¸Áî¡£ÕâÌõÖ¸Áî²»ÒªÇóCPU¶ÁÄڴ棬1Õâ¸öÊýÊÇÔÚCPUÄÚ²¿²úÉúµÄ£¬³ÆΪÁ¢¼´Êý£¨Immediate£©¡£ÔÚ»ã±à³ÌÐòÖУ¬Á¢¼´ÊýÇ°ÃæÒª¼Ó$£¬¼Ä´æÆ÷ÃûÇ°ÃæÒª¼Ó%£¬ÒÔ±ã¸ú·ûºÅÃûÇø·Ö¿ª¡£ÒÔºóÎÒÃǻῴµ½movÖ¸ÁÓÐÁíÍ⼸ÖÖÐÎʽ£¬µ«Êý¾Ý´«ËÍ·½Ïò¶¼ÊÇÒ»ÑùµÄ£¬µÚÒ»¸ö²Ù×÷Êý×ÜÊÇÔ´²Ù×÷Êý£¬µÚ¶þ¸ö²Ù×÷Êý×ÜÊÇÄ¿±ê²Ù×÷Êý¡£
movl $4, %ebx
ºÍÉÏÒ»ÌõÖ¸ÁîÀàËÆ£¬Éú³ÉÒ»¸öÁ¢¼´Êý4²¢±£´æµ½ebx¼Ä´æÆ÷ÖС£
int $0x80
Ç°Á½ÌõÖ¸ÁÊÇΪÕâÌõÖ¸Áî×ö×¼±¸µÄ£¬Ö´ÐÐÕâÌõÖ¸Áîʱ·¢ÉúÒÔ϶¯×÷£º
intÖ¸Áî³ÆΪÈíÖжÏÖ¸Á¿ÉÒÔÓÃÕâÌõÖ¸Áî¹ÊÒâ²úÉúÒ»¸öÒì³££¬ÉÏÒ»Õ½²¹ý£¬Òì³£µÄ´¦ÀíºÍÖжÏÀàËÆ£¬CPU´ÓÓû§Ä£Ê½Çл»µ½ÌØȨģʽ£¬È»ºóÌøתµ½Äں˴úÂëÖÐÖ´ÐÐÒì³£´¦Àí³ÌÐò¡£
intÖ¸ÁîÖеÄÁ¢¼´Êý0x80ÊÇÒ»¸ö²ÎÊý£¬ÔÚÒì³£´¦Àí³ÌÐòÖÐÒª¸ù¾ÝÕâ¸ö²ÎÊý¾ö¶¨ÈçºÎ´¦Àí£¬ÔÚLinuxÄÚºËÖÐint $0x80ÕâÖÖÒì³£³ÆΪϵͳµ÷Óã¨System Call£©¡£ÄÚºËÌṩÁ˺ܶàϵͳ·þÎñ¹©Óû§³ÌÐòʹÓ㬵«ÕâЩϵͳ·þÎñ²»ÄÜÏñ¿âº¯Êý£¨±ÈÈçprintf£©ÄÇÑùµ÷Óã¬ÒòΪÔÚÖ´ÐÐÓû§³ÌÐòʱCPU´¦ÓÚÓû§Ä£Ê½£¬²»ÄÜÖ±½Óµ÷ÓÃÄں˺¯Êý£¬ËùÒÔÐèҪͨ¹ýϵͳµ÷ÓÃÇл»CPUģʽ£¬¾ÓÉÒì³£´¦Àí³ÌÐò½øÈëÄںˣ¬Óû§³ÌÐòÖ»ÄÜͨ¹ý¼Ä´æÆ÷´«¼¸¸ö²ÎÊý£¬Ö®ºó¾ÍÒª°´ÄÚºËÉè¼ÆºÃµÄ´úÂë·Ïß×ߣ¬¶ø²»ÄÜÓÉÓû§³ÌÐòËæÐÄËùÓû£¬Ïëµ÷ÄĸöÄں˺¯Êý¾Íµ÷ÄĸöÄں˺¯Êý£¬ÕâÑù¿ÉÒÔ±£Ö¤ÏµÍ³·þÎñ±»°²È«µØµ÷Óá£ÔÚµ÷ÓýáÊøÖ®ºó£¬CPUÔÙÇл»»ØÓû§Ä£Ê½£¬¼ÌÐøÖ´ÐÐint $0x80µÄÏÂÒ»ÌõÖ¸ÁÔÚÓû§³ÌÐò¿´À´¾ÍÏñº¯Êýµ÷Óúͷµ»ØÒ»Ñù¡£
eaxºÍebxµÄÖµÊÇ´«µÝ¸øϵͳµ÷ÓõÄÁ½¸ö²ÎÊý¡£eaxµÄÖµÊÇϵͳµ÷Óúţ¬LinuxµÄ¸÷ÖÖϵͳµ÷Óö¼ÊÇÓÉint $0x80Ö¸ÁîÒý·¢µÄ£¬ÄÚºËÐèҪͨ¹ýeaxÅжÏÓû§Òªµ÷Äĸöϵͳµ÷Óã¬_exitµÄϵͳµ÷ÓúÅÊÇ1¡£ebxµÄÖµÊÇ´«¸ø_exitµÄ²ÎÊý£¬±íʾÍ˳ö״̬¡£´ó¶àÊýϵͳµ÷ÓÃÍê³ÉÖ®ºó»á·µ»ØÓû§¿Õ¼ä¼ÌÐøÖ´ÐкóÃæµÄÖ¸Á¶ø_exitϵͳµ÷ÓñȽÏÌØÊ⣬Ëü»áÖÕÖ¹µôµ±Ç°½ø³Ì£¬¶ø²»ÊÇ·µ»ØÓû§¿Õ¼ä¼ÌÐøÖ´ÐС£
x86»ã±àµÄÁ½ÖÖÓï·¨£ºintelÓï·¨ºÍAT&TÓï·¨ ÇëµãÆÀ
x86»ã±àÒ»Ö±´æÔÚÁ½ÖÖ²»Í¬µÄÓï·¨£¬ÔÚintelµÄ¹Ù·½ÎĵµÖÐʹÓÃintelÓï·¨£¬WindowsҲʹÓÃintelÓï·¨£¬¶øUNIXƽ̨µÄ»ã±àÆ÷һֱʹÓÃAT&TÓï·¨£¬ËùÒÔ±¾ÊéʹÓÃAT&TÓï·¨¡£movl %edx,%eaxÕâÌõÖ¸ÁîÈç¹ûÓÃintelÓï·¨À´Ð´£¬¾ÍÊÇmov eax,edx£¬¼Ä´æÆ÷Ãû²»¼Ó%ºÅ£¬Ô´²Ù×÷ÊýºÍÄ¿±ê²Ù×÷ÊýµÄλÖû¥»»£¬×Ö³¤Ò²²»ÊÇÓÃÖ¸ÁîµÄºó׺l±íʾ¶øÊÇÓÃÁíÍâµÄ·½Ê½±íʾ¡£±¾Êé²»ÏêϸÌÖÂÛÕâÁ½ÖÖÓï·¨Ö®¼äµÄÇø±ð£¬¶ÁÕß¿ÉÒԲο¼¡£
½éÉÜx86»ã±àµÄÊéºÜ¶à£¬UNIXƽ̨µÄÊ鶼²ÉÓÃAT&TÓï·¨£¬ÀýÈ磬ÆäËüÊéÒ»°ã²ÉÓÃintelÓï·¨£¬ÀýÈç¡£
Ï°Ìâ ÇëµãÆÀ
1¡¢°Ñ±¾½ÚÀý×ÓÖеÄint $0x80Ö¸ÁîÈ¥µô£¬»ã±à¡¢Á´½ÓÒ²ÄÜͨ¹ý£¬µ«ÊÇÖ´ÐеÄʱºò³öÏֶδíÎó£¬ÄãÄܽâÊÍÆäÔÒòÂð£¿
ÉÏÒ»Ò³ ÉÏÒ»¼¶ ÏÂÒ»Ò³
µÚ 18 Õ x86»ã±à³ÌÐò»ù´¡ Æðʼҳ 2. x86µÄ¼Ä´æÆ÷
È«¹úǶÈëʽÈ˲ÅÅàѵ»ùµØ
4. Ñ°Ö··½Ê½ ÇëµãÆÀ
ͨ¹ýÉÏÒ»½ÚµÄÀý×ÓÎÒÃÇÁ˽⵽£¬·ÃÎÊÄÚ´æʱÔÚÖ¸ÁîÖпÉÒÔÓöàÖÖ·½Ê½±íʾÄÚ´æµØÖ·£¬±ÈÈç¿ÉÒÔÓÃÊý×é»ùµØÖ·¡¢ÔªËس¤¶ÈºÍϱêÈý¸öÁ¿À´±íʾ£¬Ôö¼ÓÁËÑ°Ö·µÄÁé»îÐÔ¡£±¾½Ú½éÉÜx86³£Óõļ¸ÖÖÑ°Ö··½Ê½£¨Addressing Mode£©¡£ÄÚ´æÑ°Ö·ÔÚÖ¸ÁîÖпÉÒÔ±íʾ³ÉÈçϵÄͨÓøñʽ£º
ADDRESS_OR_OFFSET(%BASE_OR_OFFSET,%INDEX,MULTIPLIER)
ËüËù±íʾµÄµØÖ·¿ÉÒÔÕâÑù¼ÆËã³öÀ´£º
FINAL ADDRESS = ADDRESS_OR_OFFSET + BASE_OR_OFFSET + MULTIPLIER * INDEX
ÆäÖÐADDRESS_OR_OFFSETºÍMULTIPLIER±ØÐëÊdz£Êý£¬BASE_OR_OFFSETºÍINDEX±ØÐëÊǼĴæÆ÷¡£ÔÚÓÐЩѰַ·½Ê½ÖлáÊ¡ÂÔÕâ4ÏîÖеÄijЩÏÏ൱ÓÚÕâЩÏîÊÇ0¡£
Ö±½ÓÑ°Ö·£¨Direct Addressing Mode£©¡£Ö»Ê¹ÓÃADDRESS_OR_OFFSETÑ°Ö·£¬ÀýÈçmovl ADDRESS, %eax°ÑADDRESSµØÖ·´¦µÄ32λÊý´«Ë͵½eax¼Ä´æÆ÷¡£
±äÖ·Ñ°Ö·£¨Indexed Addressing Mode£© ¡£ÉÏÒ»½ÚµÄmovl data_items(,%edi,4), %eax¾ÍÊôÓÚÕâÖÖÑ°Ö··½Ê½£¬ÓÃÓÚ·ÃÎÊÊý×éÔªËرȽϷ½±ã¡£
¼ä½ÓÑ°Ö·£¨Indirect Addressing Mode£©¡£Ö»Ê¹ÓÃBASE_OR_OFFSETÑ°Ö·£¬ÀýÈçmovl (%eax), %ebx£¬°Ñeax¼Ä´æÆ÷µÄÖµ¿´×÷µØÖ·£¬°ÑÄÚ´æÖÐÕâ¸öµØÖ·´¦µÄ32λÊý´«Ë͵½ebx¼Ä´æÆ÷¡£×¢ÒâºÍmovl %eax, %ebxÇø·Ö¿ª¡£
»ùÖ·Ñ°Ö·£¨Base Pointer Addressing Mode£©¡£Ö»Ê¹ÓÃADDRESS_OR_OFFSETºÍBASE_OR_OFFSETÑ°Ö·£¬ÀýÈçmovl 4(%eax), %ebx£¬ÓÃÓÚ·ÃÎʽṹÌå³ÉÔ±±È½Ï·½±ã£¬ÀýÈçÒ»¸ö½á¹¹ÌåµÄ»ùµØÖ·±£´æÔÚeax¼Ä´æÆ÷ÖУ¬ÆäÖÐÒ»¸ö³ÉÔ±ÔڽṹÌåÄÚµÄÆ«ÒÆÁ¿ÊÇ4×Ö½Ú£¬Òª°ÑÕâ¸ö³ÉÔ±¶ÁÉÏÀ´¾Í¿ÉÒÔÓÃÕâÌõÖ¸Áî¡£
Á¢¼´ÊýÑ°Ö·£¨Immediate Mode£©¡£¾ÍÊÇÖ¸ÁîÖÐÓÐÒ»¸ö²Ù×÷ÊýÊÇÁ¢¼´Êý£¬ÀýÈçmovl $12, %eaxÖеÄ$12£¬ÕâÆäʵ¸úѰַûʲô¹Øϵ£¬µ«Ò²Ëã×÷Ò»ÖÖÑ°Ö··½Ê½¡£
¼Ä´æÆ÷Ñ°Ö·£¨Register Addressing Mode£©¡£¾ÍÊÇÖ¸ÁîÖÐÓÐÒ»¸ö²Ù×÷ÊýÊǼĴæÆ÷£¬ÀýÈçmovl $12, %eaxÖеÄ%eax£¬Õâ¸úÄÚ´æѰַûʲô¹Øϵ£¬µ«Ò²Ëã×÷Ò»ÖÖÑ°Ö··½Ê½¡£ÔÚ»ã±à³ÌÐòÖмĴæÆ÷ÓÃÖú¼Ç·ûÀ´±íʾ£¬ÔÚ»úÆ÷Ö¸ÁîÖÐÔòÒªÓü¸¸öBit±íʾ¼Ä´æÆ÷µÄ±àºÅ£¬Õ⼸¸öBitÒ²¿ÉÒÔ¿´×÷¼Ä´æÆ÷µÄµØÖ·£¬µ«ÊǺÍÄÚ´æµØÖ·²»ÔÚÒ»¸öµØÖ·¿Õ¼ä¡£
С¼×ÓãÀÏʦÔÚÊÓƵÖÐÒѾ˵ÁËÕâÑùµÄºÃ´¦£¡ÔÚÌض¨µÄ³¡¾°ÏÂÕâÑùÔËÐкܷ½±ã 15ËêµÄСÅóÓÑ ·¢±íÓÚ 2014-10-7 07:41
С¼×ÓãÀÏʦÔÚÊÓƵÖÐÒѾ˵ÁËÕâÑùµÄºÃ´¦£¡ÔÚÌض¨µÄ³¡¾°ÏÂÕâÑùÔËÐкܷ½±ã
½ö½öÊÇΪÁË·½±ã£¿
ÕâÑùµÄÀíÓɲ»Ì«³ä·Ö¡£
¾ÝÎÒËùÖª£¬MIPSÖ¸Á¾ÍÊÇÓÃµÄ »ùµØÖ·Ö¸ÏòÆðʼ£¬Æ«ÒƵØÖ·Ö¸Ïòµ¥¸öÔªËØ¡£
±ðÈËÒ»ÑùºÜ·½±ã¡£ ºÃ¶à£¬ºÃºÃѧϰ°É ѧϰѧϰ
Ò³:
[1]