【汇编】用栈传参的案例及分析
创始人
2024-06-02 00:47:53
0

目录

案例分析

案例一代码

分析

案例二代码

知识补充


案例分析

案例一代码

bubbleSort proc
;bubbleSort(int *arr,int len) call C style
;input:
;push len
;push offset arrpush ebpmov ebp,esppushadmov edx,[ebp+8]mov ecx,[ebp+12]mov  esi,0;i=esisub  ecx,1;ecx=n-1
again_1: cmp  esi,ecx jge  final_1mov  edi,0;j=edimov  ebx,ecxsub  ebx,esi
again_2: cmp  edi,ebx;ebx=n-1-i jge  final_2mov  eax,[edx+4*edi]cmp  eax,[edx+4*edi+4]jle  nextpush [edx+4*edi+4]pop  [edx+4*edi]mov  [edx+4*edi+4],eax
next:	 inc  edijmp   again_2
final_2:	 inc esijmp again_1
final_1: popadpop ebpret 8
bubbleSort endpoutput proc
;output(int *arr ,int len)
;input:
;edx=arr
;ecx=lenpushadmov esi,0
again:	cmp esi,ecxjge finalmov eax,[edx+4*esi]call writeintmov  al,' 'call writecharinc  esijmp again
final:	popadret
output endp

分析

push len
push offset arr

先将长度和首地址进行入栈

push ebp
mov ebp,esp
pushad

将ebp进栈方便对len和arr进行寻址,将各寄存器进栈

mov edx,[ebp+8]
mov ecx,[ebp+12]

此时edx,ecx分别为arr首地址和长度

进行逻辑运算后

popad
pop ebp

ret 8

将各寄存器出栈,将ebp出栈,ret 8让栈平衡

案例二代码

  • 源代码
void insertionSort( int arr[], int n){
int i;
for(i = 1; i < n; i++) {
insert(arr, i);
}
}void insert ( int arr[], int n){
int key = arr[n];
int i = n;
while (arr[i - 1] > key) {
arr[i] = arr[i-1];
i--;
if(i == 0) break;
}
arr[i] = key;
}
  • 汇编代码 
INCLUDE Irvine32.inc
.data
arr dd 99, 2, 3, -11, 22, 88, 7, 77, 547, 717, -54
len dd ($-arr)/4
a dd ?
.code
insert PROC
;arr,a(i)
push ebp
mov ebp,esp
pushad
mov ebx,[ebp+8];ebx=i(n)
mov eax,[ebp+12];eax=arr
mov ecx,[eax+ebx*4];ecx=key
again:
cmp [eax+4*ebx-4],ecx
jle final
mov edx,[eax+4*ebx-4]
mov [eax+4*ebx],edx
sub ebx,1
cmp ebx,0
je final
jmp again
final:mov [eax+4*ebx],ecxpopadpop ebpret 8
insert ENDPmain PROC
mov a,1
again:
mov eax,a;a为下标(eax)
cmp eax,len;(len=n)
jae myout
push offset arr
push a
call insert
add a,1
jmp againmyout:mov ebx,0again2:cmp ebx,lenjae final2mov eax,arr[ebx*4]call WriteIntadd ebx,1jmp again2final2:exit
main ENDP
END main

知识补充

  • 寄存器批量进(出)栈

  •  数据交换

  • 换行函数

call crlf

相关内容

热门资讯

中证A500ETF摩根(560... 8月22日,截止午间收盘,中证A500ETF摩根(560530)涨1.19%,报1.106元,成交额...
A500ETF易方达(1593... 8月22日,截止午间收盘,A500ETF易方达(159361)涨1.28%,报1.104元,成交额1...
何小鹏斥资约2.5亿港元增持小... 每经记者|孙磊    每经编辑|裴健如 8月21日晚间,小鹏汽车发布公告称,公司联...
中证500ETF基金(1593... 8月22日,截止午间收盘,中证500ETF基金(159337)涨0.94%,报1.509元,成交额2...
中证A500ETF华安(159... 8月22日,截止午间收盘,中证A500ETF华安(159359)涨1.15%,报1.139元,成交额...
科创AIETF(588790)... 8月22日,截止午间收盘,科创AIETF(588790)涨4.83%,报0.760元,成交额6.98...
创业板50ETF嘉实(1593... 8月22日,截止午间收盘,创业板50ETF嘉实(159373)涨2.61%,报1.296元,成交额1...
港股异动丨航空股大幅走低 中国... 港股航空股大幅下跌,其中,中国国航跌近7%表现最弱,中国东方航空跌近5%,中国南方航空跌超3%,美兰...
电网设备ETF(159326)... 8月22日,截止午间收盘,电网设备ETF(159326)跌0.25%,报1.198元,成交额409....
红利ETF国企(530880)... 8月22日,截止午间收盘,红利ETF国企(530880)跌0.67%,报1.034元,成交额29.0...