博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
汇编2——完整的例子集合
阅读量:7199 次
发布时间:2019-06-29

本文共 4154 字,大约阅读时间需要 13 分钟。

//countString.asm 是用来自动统计字符串长度,然后输出该字符串[08:12:56] vi countString.asm[08:13:28] nasm -f elf64 countString.asm[08:13:30] ld -o countString countString.o[08:13:32] ./countString hello,linux world,and hello asm[08:13:35] cat countString.asmsection .datamsg db 'hello,linux world,and hello asm',0Ahsection .textglobal _start_start:mov ebx,msg     ;use ebx,because then need sub eax,ebxmov eax,ebxnextchar:cmp byte[eax],0     ;compare whether is endjz finished     ;is end,go to finishedinc eax     ;countjmp nextchar    ;continuefinished:sub eax,ebx     ;string length eax=ebx-eaxmov edx,eax     ;edx is used to save lengthmov ecx,msg     ;make ecx become msg contentmov ebx,1       ;write to stdoutmov eax,4       ;system call writeint 80hmov ebx,0       ;return statusmov eax,1       ;system call exitint 80h

那么现在我们来换个法子显示字符串。有一点需要注意,寄存器的名称我全部改为rax,rbx等以r开头的,这个是64位寄存器的意思,eax之类的是32位,ax的应该是16位了,注释那个我就懒得改过来了

[08:42:58] nasm -g -f elf64 countString.asm[08:43:00] ld -o countString countString.o[08:43:01] ./countString hello,linux world,and hello asm![08:43:03] cat countString.asmsection .datamsg db 'hello,linux world,and hello asm!',0Ahsection .textglobal _start_start:mov rbx,msg     ;use ebx,because then need sub eax,ebx          call strlen     ;call our function to calculate the length of the string,类似c语言的函数之类的mov rdx,rax     ;edx is used to save lengthmov rcx,msg     ;make ecx become msg contentmov rbx,1       ;write to stdoutmov rax,4       ;system call writeint 80hmov rbx,0       ;return statusmov rax,1       ;system call exitint 80hstrlen:push rbx        ;push rbx,because then need rbxmov rax,rbxnextchar:cmp byte[rax],0     ;compare whether is endjz finished     ;is end,go to finishedinc rax     ;countjmp nextchar    ;continuefinished:pop rbx         ;get rbxsub rax,rbx     ;string length eax=ebx-eaxret         ; return to where the function was called

接着我们来试试包含外部文件

;functions.asm ; int slen(String message); String length calculation functionslen:    push    ebx    mov     ebx, eaxnextchar:    cmp     byte [eax], 0    jz      finished    inc     eax    jmp     nextcharfinished:    sub     eax, ebx    pop     ebx    ret; void sprint(String message); String printing functionsprint:    push    edx    push    ecx    push    ebx    push    eax    call    slen    mov     edx, eax    pop     eax    mov     ecx, eax    mov     ebx, 1    mov     eax, 4    int     80h    pop     ebx    pop     ecx    pop     edx    ret; void exit(); Exit program and restore resourcesquit:    mov     ebx, 0    mov     eax, 1    int     80h    ret;--------------------------------------------------------;--------------------------------------------------------;helloworld-inc.asm%include        'functions.asm'                             ; include our external fileSECTION .datamsg1    db      'Hello, brave new world!', 0Ah              ; our first message stringmsg2    db      'This is how we recycle in NASM.', 0Ah      ; our second message stringSECTION .textglobal  _start_start:    mov     eax, msg1       ; move the address of our first message string into EAX    call    sprint          ; call our string printing function    mov     eax, msg2       ; move the address of our second message string into EAX    call    sprint          ; call our string printing function    call    quit            ; call our quit function;---------------------------------------------------在第一个fuctions.asm的基础上修改helloworld-inc.asm,代码如下,dec减去,ecx是用来保存程序参数的,相乘mul  ebx,multiply eax by ebx%include        'functions.asm'SECTION .textglobal  _start_start:    pop     ecx             ; first value on the stack is the number of argumentsnextArg:    cmp     ecx, 0h         ; check to see if we have any arguments left    jz      noMoreArgs      ; if zero flag is set jump to noMoreArgs label (jumping over the end of the loop)    pop     eax             ; pop the next argument off the stack    call    sprintLF        ; call our print with linefeed function    dec     ecx             ; decrease ecx (number of arguments left) by 1    jmp     nextArg         ; jump to nextArg labelnoMoreArgs:    call    quit

转载于:https://www.cnblogs.com/biaopei/p/7730648.html

你可能感兴趣的文章
javascript中if和switch,==和===详解
查看>>
python写api接口测试之tonador
查看>>
ibatis初识
查看>>
hdu 1950 Bridging signals
查看>>
poj - 1442 Black Box
查看>>
js replaceChild
查看>>
个人简介
查看>>
Ubuntu卡在Logo界面
查看>>
DjangoRestFramework 学习之restful规范 APIview 解析器组件 Postman等
查看>>
Ext JS 4.1 RC2 Released发布
查看>>
一个IT经理眼中的RTX、Simba2013与Lync
查看>>
mysql数据库批量插入数据shell脚本实现
查看>>
Virtual Machine Manager 2008 2008 R2系列之安装部署
查看>>
例解三层交换原理
查看>>
【灵性觉醒】解忧杂货店感悟
查看>>
"log_bin.index not found" 启动报错解决
查看>>
SFB 项目经验-36-分配公网证书 For SFB 2015-前端服务器(图解)
查看>>
SFB 项目经验-52-Outlook-2010/2013-连接Exchange 2016需要密码!
查看>>
白盒测试不是测试,更不高级
查看>>
思科防火墙PIX8.0 L2L***解决地址重叠测试(3)
查看>>