汇编基础11--win32汇编

Catalogue
  1. 1. 汇编基础11 – win32汇编
    1. 1.0.1. 控制台程序的输入输出
      1. 1.0.1.1. 消息框
      2. 1.0.1.2. C的库函数
      3. 1.0.1.3. 示例:输入两个数,输出两数之和

汇编基础11 – win32汇编

控制台程序的输入输出

消息框

API:(Application Programming Interface,应用程序接口)是一些预先定义的函数,或指软件系统不同组成部分衔接的约定。 [1] 目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问原码,或理解内部工作机制的细节。

win32汇编是通过使用Windows提供的接口进行编程

示例:hello world!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
.586
.MODEL flat,stdcall

includelib user32.lib
includelib kernel32.lib

;声明函数
ExitProcess PROTO, dwExitCode:DWORD
MessageBoxA PROTO, hWnd:DWORD ,lpText:BYTE ,lpCaption:BYTE ,uType:DWORD
;参数从栈中调用出来,左边的参数在栈下面,右边的在上面

.data
str1 db "Hello world" ,0;需要,0结尾,不然出错
;db是一个占了8位的类型,db后面的内容视为数据而不视为代码

.code
main proc
push 1
lea eax ,str1
push eax
push eax
push 0
call MessageBoxA
add esp ,16 ;堆栈平衡
invoke ExitProcess,0
main ENDP
END main

最常用的一个工具是MASM32,网上搜索,下载,安装

找到目录下的include,和lib目录添加到配置里:

在vs2015里右键项目名称–属性:

  • 链接器–常规–附加库目录
  • Microsoft Macro Assmble–General–include Path

C的库函数

输出用到的库:msvcrt

1
2
3
4
5
6
7
8
9
10
11
include msvcrt.inc
includelib msvcrt.lib
.data
text db "hello world !",0
.code
main proc
push offset text
call crt_printf
add esp ,4
main endp
END main

输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
include msvcrt.inc
includelib msvcrt.lib
.data
format db "%s",0
text1 db 0
.code
main proc
push offset text1
push offset format
call crt_scanf ;输入
call crt_printf
add esp ,8
main endp
END main

示例:输入两个数,输出两数之和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
.586
.model flat ,stdcall
option casemap:none
include kernel32.inc
include user32.inc
include msvcrt.inc

includelib kernel32.lib
includelib user32.lib
includelib msvcrt.lib

.data
text db "请输入两个数:",0dh,0ah,0
text1 db "两个数的和为:",0
format db "%s",0
format_num db "%d",0
num1 db 0,0,0,0
num2 db 0,0,0,0

.code
main proc
push offset text
call crt_printf
add esp ,4

push offset num1
push offset format_num
call crt_scanf
add esp ,8

push offset num2
push offset format_num
call crt_scanf
add esp ,8

mov eax ,dword ptr num1
add eax ,dword ptr num2
push eax
push offset format_num


push offset text1
push offset format
call crt_printf
add esp ,8

call crt_printf
add esp ,8

push 0
call ExitProcess
add esp ,4

main endp
END main