汇编(32位)总结2

框架

N equ 100
.data
	msg1 db 'YES',0
	a dword 1,4,23,53,654,12,1,7,8,123
	b dword 10 dup(?)
	len dword 9
.code
	main proc

		exit
	main endp
	    ;函数
	end main

读写

;readstring
.data
	expr db 128 dup(0),0
.code
	lea edx,expr
	mov ecx,128
	call readstring
	mov len,eax

;读写
	call readint
	mov a,al
	movsz eax,a  ;带符号扩展
	movzx eax,a  ;无符号扩展
	call writedec
	call crlf

	lea edx,str1
	call writestring

函数

;有return
fff proc
	push ebp
	mov ebp,esp
	sub esp,4
	pushad
	......
	...mov [ebp-4],ebx
	......
	popad
	mov eax,[ebp-4]
	pop ebp
	ret x
fff endp

;swap
;push offset a  :[ebp+8]
;push offset b  :[ebp+12]
;call swap
;return nothing
swap proc
    push ebp
    mov ebp,esp
    pushad
    mov ebx,[ebp+12]    ;存放的是地址
    mov eax,[ebx]    ;把地址的值传给eax [[ebp+12]]
    mov ecx,[ebp+8]     ;ecx = offset b
    mov edx,[ecx]    ;[[ebp+8]]
    mov [ebx],edx
    mov [ecx],eax
    popad
    pop ebp
	ret 8
swap endp

comm

二维数组

.data
arr byte .....
;arr[1][2]=[arr+1*5*1+2*1]
;arr[2][3]=[arr+2*5*1+3*1]  byte每个元素占一个字节
;arr[i][j]=[arr+i*5*1+j*1]

;movzx eax,[arr+5*esi+edi]       
;[arr+i*5*1+j*1]  
;[const+reg1+k*reg2] k=0,1,2,4,8 只有const(常数)可以为负数
mov eax,esi
mov ebx,5
mul ebx   
;ebx=5*exi
movzx eax,[arr+eax+edi] 
call writedec

请添加图片描述stringOps

include irvine32.inc
.data
 msg1 db 'The Sysinternals utilities cover a broad range of functionality across many'
     db 'aspects of the Windows operating system. While some of the more comprehensive'
     db 'utilities such as Process Explorer and Process Monitor span several categories'
     db 'of operations, others can more or less be grouped within a single category, '
     db 'such as process utilities or file utilities. Many of the utilities have a'
     db 'graphical user interface (GUI), while others are console utilities with rich'
     db 'command-line interfaces designed for automation or for use at a command prompt.',0

msg2 db 200 dup('X'),0
num  dword 012345678h ;十六進制
msg3 db 'The sysinternals utilities cover a broad range of functionality across Many',0
expr db '123 * 45 - 566 + 142 / 42',0
len  dword ($-expr)

.code
start:
	lea edx,msg2
	call writestring
	call crlf
;;STOSB/STOSW/STOSD使用示例
	lea edi,msg2
	mov ecx,200;;ecx=200
	mov al,'A'
	cld
	rep stosb

	lea  edx,msg2
	call writestring
	call crlf

	lea  edi,msg2
	mov  ecx,100;;ecx=100
	mov  al,'A'
	mov  ah,'B'
	cld
	rep  stosw

	lea  edx,msg2
	call writestring
	call crlf

	lea edi,msg2
	mov ecx,50  ;;ecx=50
	mov al,'C'
	mov ah,'D'
	shl eax,16;左移16位
	mov al,'A'
	mov ah,'B'
	cld
	rep stosd

	lea edx,msg2
	call writestring
	call crlf
;;MOVSB/MOVSW/MOVSD使用示例
	lea  esi,msg1
	lea  edi,msg2
	mov  ecx,200
	cld
	rep  movsb
	lea  edx,msg2
	call writestring
	call crlf

	lea  esi,msg1
	lea  edi,msg2
	mov  ecx,100
	cld
	rep  movsw
	lea  edx,msg2
	call writestring
	call crlf

	lea esi,msg1
	lea edi,msg2
	
	mov  eax,esi
	call writehex
	mov  al,' '
	call writechar
	mov  eax,edi
	call writehex
	call crlf

	mov ecx,50
	cld
	rep  movsd
	lea  edx,msg2
	call writestring
	call crlf

	mov  eax,esi
	call writehex
	mov  al,' '
	call writechar
	mov  eax,edi
	call writehex
	call crlf
;LODSB/LODSW/LODSD使用示例
	lea esi,num
	lodsb
	movzx eax,al
	call writehex
	call crlf

	lea esi,num
	lodsw
	movzx eax,ax
	call writehex
	call crlf
	
	lea esi,num
	lodsd
	;movzx eax,ax
	call writehex
	call crlf
;CMPSB/CMPSW/CMPSD使用示例
	lea esi,msg1
	lea edi,msg3
	
	mov eax,esi
	call writehex
	mov al,' '
	call writechar
	mov eax,edi
	call writehex
	call crlf
	mov ecx,200
	cld
	repz cmpsb
	mov  eax,esi
	call writehex
	mov  al,' '
	call writechar
	mov  eax,edi
	call writehex
	call crlf

	lea esi,msg1
	lea edi,msg3
	
	mov  eax,esi
	call writehex
	mov  al,' '
	call writechar
	mov  eax,edi
	call writehex
	call crlf
	mov  ecx,100
	cld
	repz cmpsw
	mov  eax,esi
	call writehex
	mov  al,' '
	call writechar
	mov  eax,edi
	call writehex
	call crlf
	lea  esi,msg1
	lea  edi,msg3
	
	mov  eax,esi
	call writehex
	mov  al,' '
	call writechar
	mov  eax,edi
	call writehex
	call crlf
	mov  ecx,50
	cld
	repz cmpsd
	mov  eax,esi
	call writehex;十六進制
	mov  al,' '
	call writechar
	mov  eax,edi
	call writehex ;十六進制
	call crlf

;SCASB/SCASW/SCASD使用示例
	lea  edi,expr
	mov  eax,edi
	call writedec; ;十進制
	mov  al,' '
	call writechar
	mov  al,'+'
	mov  ecx,len
	cld
	repnz scasb
	mov  eax,edi
	call writedec;十進制
	call crlf
	exit
end start

串处理指令
MOVS、 STOS、 LODS 、 CMPS、 SCAS
设置方向标志指令
CLD、 STD
串重复前缀
REP、 REPE / REPZ、 REPNE / REPNZ

栗子

进制的转换


title 实验一 程序结构示例
;分支结构,循环结构,子程序
include irvine32.inc
.data
	msg1 db '1)2进制',0
	msg2 db '2)8进制',0
	msg3 db '3)10进制',0
	msg4 db '4)16进制',0
	msg5 db '请输入选项:',0
	msg6 db '请输入无符号数:',0
	msgs dword offset msg1
	     dword offset msg2
	     dword offset msg3
	     dword offset msg4
	     dword offset msg5
.code
main proc 	
again:	call crlf
	mov  ecx,5
	lea  ebx,msgs
	call showMenu
       	call readint
	cmp  eax,1
	jz   L1
	cmp  eax,2
	jz   L2
	cmp  eax,3
	jz   L3
	cmp  eax,4
	jz   L4
	jmp  final
L1:     call f1
	jmp  final
L2:     call f2
	jmp  final
L3:     call f3
	jmp final
L4:     call f4
	jmp final
final:	call crlf
	jmp again
	exit
main  endp

showMenu proc
;showMenu(msgs,len)
;msgs=ebx,len=ecx
	push esi
	push edx
	mov  esi,0 ;i=esi
again:  cmp  esi,ecx
	jge  final
	mov  edx,[ebx+4*esi]
	call writestring
	call crlf
	inc  esi
	jmp  again
final:  pop edx
	pop  esi
	ret
showMenu endp

f1  proc
	push edx
	lea  edx,msg6
	call InputInt
	call writebin
	pop  edx
	ret
f1  endp                              

f2 proc
	push edx
	lea  edx,msg6
	call InputInt
	call writeOct
	pop  edx
	ret
f2 endp

f3 proc
	push edx
	lea  edx,msg6
	call InputInt
	call writeDec
	pop  edx
	ret
f3 endp

f4 proc
	push edx
	lea  edx,msg6
	call InputInt
	call writeHex
	pop  edx
	ret
f4 endp

InputInt proc
;receives: edx=msg6
;return:   eax=integer from keybord
	call  writestring
	call  crlf
	call  readint
	ret
InputInt endp

writeOct proc
;receives: eax
;return:nothing
	push ebp
	mov  ebp,esp
	sub  esp,80 ;int r[20]
	push esi
	push ebx
	push eax
	mov esi,0 ;i=0
	mov ebx,8 
again_1:cmp eax, 0
	jle again_2
	mov edx,0 
	div ebx
	mov [ebp+4*esi-80],edx;r[i]=num%8
	inc esi
	jmp again_1
again_2: cmp esi,0
	jle  final
	mov  eax,[ebp+4*esi-4-80];r[i-1]
	call writedec
	dec  esi
	jmp  again_2
final:  pop  eax
	pop  ebx
	pop  esi
	add  esp,80
	pop  ebp
	ret
writeOct endp                          
end main

;显示八进制的算法:
writeOct(int num):
int r[20]
int i=0
while(num>0){
	r[i]=num%8
	num=num/8
	i++
}
while(i>0){
   output(r[i-1])
   i--
}

79/8  7  r[0]
9/8   1  r[1]
1/8   1  r[2]
0/       i=3

字符串划分计算

include irvine32.inc
.data
	expr  db 100 dup (?)
	expr1 db 20  dup (?)
	expr2 db 20  dup (?)
	len   dd ?
	ops   db '+-*/',0
;"  1234  +   5678  "
.code
main proc
	lea  edx,expr
	mov  ecx,100  ;<128
	call readstring;
	mov  len,eax;eax=实际读到的字符个数

	push offset expr
	push len
	push offset ops
	push offset expr1
	push offset expr2
	call splitExpr
	lea  edx,expr1
	call writestring
	call crlf
	lea  edx,expr2
	call writestring
	call crlf

	exit
main endp
;======================
;push offset expr :[ebp+24]
;push len      :  [ebp+20]
;push offst ops:  [ebp+16]
;push offset expr1:[ebp+12]
;push offset expr2:[ebp+8]
;call splitExpr
;return: eax=运算符在ops中的位置
splitExpr proc
	push ebp
	mov  ebp,esp
	sub  esp,4
	pushad
	mov  edx,0;edx=k
	mov  ebx,[ebp+16];ebx=offset ops
again:  cmp  byte ptr [ebx+edx],0
	jz   final
	;若ops[k] 在expr 中,设其位置为p 
	mov al,byte ptr [ebx+edx]
	mov edi,[ebp+24];edi=offset expr
	mov ecx,[ebp+20];ecx=len
	cld
	repnz scasb
	;"  1234  +   5678  ",0;"   +2343"
	;1.从expr前部子串的第一个非空格字符开始, 将子串其余字符拷贝到expr1
	cmp ecx,0
	jz  next
	push edi
	push ecx
	mov  eax,[ebp+20]
	sub  eax,ecx;=len-ecx
	dec  eax
	mov  ecx,eax
	mov  al,' '
	mov  edi,[ebp+24];edi=offset expr
	cld
	repz scasb
	dec  edi
	inc  ecx
	cmp byte ptr[edi],' '
	jz  nul_str1
	mov esi,edi
	mov edi,[ebp+12]
	cld
	rep movsb
	mov byte ptr[edi],0
	;2.从expr后部子串的第一个非空格字符开始,  将子串其余字符拷贝到expr2

	pop ecx
	pop edi
	mov al,' '
	cld 
	repz scasb

	dec  edi
	inc  ecx
	cmp  byte ptr[edi],' '
	jz   nul_str2
	mov  esi,edi
	mov  edi,[ebp+8]
	cld
	rep movsb
	mov byte ptr[edi],0
	jmp final
next:	
        inc edx
	jmp again

nul_str1:pop ecx
	 pop edi
	 mov ebx,[ebp+12]
	 mov byte ptr[ebx],0

nul_str2:mov ebx,[ebp+8]
	 mov byte ptr[ebx],0
final:
	mov [ebp-4],edx
	popad
	mov  eax,[ebp-4]
	add  esp,4
	pop  ebp
	ret  20
splitExpr endp
;============================
;push offset expr
;call trimStr
;return :nothing
trimStr proc
	push ebp
	mov  ebp,esp
	pushad
	mov ebx,[ebp+8]
	mov esi,0
	'123',0
again:  cmp byte ptr[ebx+esi],0
	jz  final
	mov al,byte ptr[ebx+esi]
	cmp al,'0'
	jl  BADSTR
	cmp al,'9'
	jg  BADSTR
	inc esi
	jmp again
BADSTR: mov byte ptr[ebx+esi],0
final:	popad
	pop  ebp
	ret 4
trimStr endp
;====================================
end main

 
 step 1.输入含简单算术表达式的字符串expr 
       例如 "  1234  +   5678  ",0
	
step 2. splitExpr: 用运算符将 expr 分割为含数字
	字符的俩个字符串expr1与expr2 (以0结束)
	s1 :     "  1234  +   5678  ",0;scasb
	==>      "  1234  +"
	         "  5678  ",0
	s2 :     "  1234  +" ==>
		 "  1234 ",0 ==>
		 "12 34 ",0 --->>>> expr1 
		 "  5678  ",0==>
		 "5678  ",0 --->>>>expr2

int splitExpr(expr*,len,ops[],expr1*,expr2*){ops='+-*/',0
	for each k in  {0,1,2,3}{
		若ops[k] 在expr 中,设其位置为p 则{
		  1.从expr前部子串的第一个非空格字符开始,
		    将子串其余字符拷贝到expr1
		  2.从expr后部子串的第一个非空格字符开始,
		    将子串其余字符拷贝到expr2
		    return k;
		}
	}
	return 4;
}

step 3.

void trimStr( expr){
     删除expr中第一个非数字字母及其后的字符;
}

step 4.
unsinged str2Int (expr)
    将expr转换成无符号的整数
}

示例:
  123+456  
  123  +  456
  123+ 
   + 456
   1d24 + 567

汉诺塔

include  irvine32.inc
.data
.code
main proc
	push 3
	mov  al, 'A'
	push eax
	mov  al,'B'
	push eax
	mov  al,'C'
	push eax
	call  Hanoi
	exit
main endp

;push x
;push y
;call Move
;return:nothing
Move proc
	push ebp
	mov  ebp,esp
	pushad
	mov  eax,dword ptr[ebp+12];x
	call writechar
	lea  edx,arrow
	call writestring
	mov  eax,dword ptr[ebp+8];y
	call writechar
	call crlf
	
	popad
	pop ebp
	ret 8
arrow   db '-->',0
Move endp
;push n    :[ebp+20]
;push one  :[ebp+16]
;push two  :[ebp+12]
;push three :[ebp+8]
;call Hanoi
;return:nothing
Hanoi proc
	push ebp
	mov  ebp,esp
	pushad
	mov eax,[ebp+16];one
	mov ebx,[ebp+12]
	mov ecx,[ebp+8]
	mov edi,[ebp+20];n
	cmp  edi,1;n==1
	jnz  next
	push eax
	push ecx
	call Move
	jmp final
next:
	dec  edi
	push edi
	push eax
	push ecx
	push ebx
	call Hanoi

	push eax
	push ecx
	call Move

	push edi
	push ebx
	push eax
	push ecx
	call Hanoi

final:
	popad
	pop  ebp
	ret  16
Hanoi endp

end main
======================================
;push x
;push y
;call Move
;return:nothing
Move proc
	push ebp
	mov  ebp,esp
	pushad
	mov  eax,dword ptr[ebp+12];x
	call writechar
	lea  edx,arrow
	call writestring
	mov  eax,dword ptr[ebp+12]
	call writechar
	call crlf
	
	popad
	pop ebp
	ret 8
arrow   db '-->',0
Move endp

void Move(char x,char y){
	print("%c-->%c",x,y);
}

;push n    :[ebp+20]
;push one  :[ebp+16]
;push two  :[ebp+12]
;push three :[ebp+8]
;call Hanoi
;return:nothing
Hanoi proc
	push ebp
	mov  ebp,esp
	pushad
	mov  eax,[ebp+16];one
	mov  ebx,[ebp+12]
	mov  ecx,[ebp+8]
	mov  edi,[ebp+20];n
	cmp  edi,1;n==1
	jnz  next
	push eax
	push ecx
	call Move
	jmp final
next:
	dec  edi
	push edi
	push eax
	push ecx
	push ebx
	call Hanoi

	push eax
	push ecx
	call Move

	push edi
	push ebx
	push eax
	push ecx
	call Hanoi

final:
	popad
	pop  ebp
	ret  16
Hanoi endp

void Hanoi(int n, char one, char two,char three){
	if(n==1) move(one,three)
	else{
		Hanoi(n-1,one,three,two);
		Move(one,three);
		Hanoi(n-1,two,one,three);

	}
}

int main(){
	int n;
	printf("Input Your Number");
	scanf("%d",&n);
	Hanoi(n,'A','B','C')
}

A-->C
A-->B
C-->B
A-->C
B-->A
B-->C
A-->C

快排

include irvine32.inc
.data
	arr dd  100000 dup(?)
	len dd  100000
	a   dd  100
	b   dd  200
.code
main proc
	push offset arr
	push len
	call GenInts
	
	push offset arr
	push 0
	mov  eax,len
	dec  eax
	push eax
	call quickSort

	push offset arr
	push len
	call print
	
	exit
main endp
;======================
;push offset a:[ebp+12]
;push len     :[ebp+8]
;call GenInts
;return :nothing
GenInts proc
	push ebp
	mov  ebp,esp
	pushad
	mov  ebx,[ebp+12];offset a
	mov  esi,0;j=esi
again:	cmp  esi,dword ptr [ebp+8];j<n?
	jge  final
	
	mov  eax,10000
	call RandomRange
	mov  [ebx+4*esi],eax

	inc  esi
	jmp  again
final:
	popad
	pop  ebp
	ret  8;
GenInts endp
;======================
;push offset a:[ebp+12]
;push len     :[ebp+8]
;call print
;return :nothing
print proc
	push ebp
	mov  ebp,esp
	pushad
	mov  ebx,[ebp+12]
	mov  esi,0;j=esi
again:
	cmp  esi,dword ptr [ebp+8];j<n?
	jge  final

	mov  eax,[ebx+4*esi]
	call writeint
	mov  al,' '
	call writechar

	inc  esi
	jmp  again
final:
	popad
	pop  ebp
	ret  8;
print endp
;==========================
;push offset a :[ebp+12]
;push offset b :[ebp+8]
;call swap
;return:nothing
swap proc
	push ebp
	mov  ebp,esp
	pushad
	mov  ebx,[ebp+12];ebx=offset a
	mov  eax,[ebx];tmp=*a
	mov  ecx,[ebp+8];ecx=offset b
	mov  edx,[ecx]
	mov  [ebx],edx
	mov  [ecx],eax
	popad
	pop  ebp
	ret 8
swap endp
;=======================================
;push offset a :[ebp+16]
;push low      :[ebp+12]
;push high     :[ebp+8]
;call partition
;return: eax =low
partition proc
	push ebp
	mov  ebp,esp
	sub  esp,4;暂存返回值
	pushad
	mov  ebx,[ebp+16];ebx=offset a
	mov  esi,[ebp+12];esi=low
	mov  edi,[ebp+8] ;edi=high
	mov  eax,[ebx+4*esi] ;eax=key=a[low]
again:
	cmp esi,edi;low<high?
	jge final
	;while(low < high && a[high] >= Key) --high;
	again_1:
		cmp esi,edi
		jge final_1
		cmp [ebx+4*edi],eax;a[high] >= Key?
		jl  final_1
		dec edi
		jmp again_1
	final_1:
	;swap(&a[low], &a[high]);
	lea  edx,[ebx+4*esi] ;mov edx,offset [ebx+4*esi]XXX
	push edx
	lea  edx,[ebx+4*edi]
	push edx
	call swap
	;while(low < high && a[low] <= Key ) ++low;
	again_2:
		cmp esi,edi
		jge final_2
		cmp [ebx+4*esi],eax;a[low] <= Key?
		jg  final_2
		inc esi
		jmp again_2
	final_2:
	lea  edx,[ebx+4*esi] ;mov edx,offset [ebx+4*esi]XXX
	push edx
	lea  edx,[ebx+4*edi]
	push edx
	call swap
	jmp  again
final:
	mov  [ebp-4],esi;
	popad
	mov  eax,[ebp-4]
	add  esp,4
	pop  ebp
	ret  12
partition endp
;;push low	:[ebp+12]
;push high      :[ebp+8]
;call quickSort
;return:nothing
quickSort proc
	push ebp
	mov  ebp,esp
	pushad
	mov esi,[ebp+12];esi=low
	mov edi,[ebp+8]; edi=high
	mov ebx,[ebp+16]; ebx=offset a
	cmp esi,edi;low<high
	jge final
	push ebx
	push esi
	push edi
	call partition;eax=loc
	push ebx
	push esi
	dec  eax
	push eax
	call quickSort
	push ebx
	add  eax,2
	push eax
	push edi
	call quickSort
final:
	popad
	pop  ebp
	ret 12
quickSort endp
end main
;==========

void print(int a[], int n){
	for(int j= 0; j<n; j++){
	        cout<<a[j] <<" ";
	}
	cout<<endl;
}


void swap(int *a, int *b){
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

int partition(int a[], int low, int high){
	int Key = a[low]; 
	while(low < high){ 
		while(low < high && a[high] >= Key) --high; 
		swap(&a[low], &a[high]);
		while(low < high && a[low] <= Key ) ++low;
		swap(&a[low], &a[high]);
	}
	;print(a,10);
	return low;
}
;push offset a  :[ebp+16]

void quickSort(int a[], int low, int high){
	if(low < high){
		int Loc = partition(a, low, high); //将表一分为二
		quickSort(a, low, Loc -1); //递归对低子表递归排序
		quickSort(a, Loc + 1, high); //递归对高子表递归排序
	}
}

int main(){
	int a[10] = {3,1,5,7,2,4,9,6,10,8};
	cout<<"初始值:";
	print(a,10);
	quickSort(a,0,9);
	cout<<"结果:";
	print(a,10);

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值