80x86汇编语言笔记
参考资料
80x86汇编语言与计算机体系结构
分享一个学校的汇编编译器,很好用
数组找最大值
从键盘输入10个数据,存入一维数组中,并求出该数组中最大的数并显示在屏幕上。
;Program:
;Author:
;Date:
.386
.MODEL FLAT
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
INCLUDE io.h ; header file for input/output
cr EQU 0dh ; carriage return character
Lf EQU 0ah ; line feed
.STACK 4096 ; reserve 4096-byte stack
.DATA ; reserve storage for data
intro1 BYTE ‘this program will tell you the max which is in you input’,cr,Lf
BYTE ‘please input numbers: ‘,0
intro2 BYTE ‘next:’,0
numArr DWORD 20 dup (?)
numElts DWORD 20 dup (?)
midnum DWORD 20 dup (?)
maxnum DWORD 20 dup (?)
.CODE ; start of main program code
_start:
output intro1 ;输出提示1
mov numElts,0 ;初始化数组下标
mov maxnum,0 ;初始化最大值
lea ebx,numArr
whilePos:
input midnum,20
atod midnum
mov [ebx],eax ;将转换好的数传入当前数组的地址
cmp maxnum,eax ;判断eax是否是当前最大,不是则到asusual
jle setmax
jmp asusual
setmax: mov maxnum,eax ;是则将最大值传给maxnum
asusual: inc numElts ;增加数组下标
cmp numElts,10 ;判断是否达到10个数
jge endWhile
add ebx,4 ;增加数组地址
output intro2
jmp whilePos
endWhile: ;输出最大值
mov eax, maxnum
dtoa maxnum,eax
output maxnum
INVOKE ExitProcess, 0 ; exit with return code 0
PUBLIC _start ; make entry point public
END ; end of source code
统计平均值
从键盘输入15个数据,存入一维数组中,统计其中大于平均值的数据并显示在屏幕上。
;Program:
;Author:
;Date:
.386
.MODEL FLAT
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
INCLUDE io.h ; header file for input/output
cr EQU 0dh ; carriage return character
Lf EQU 0ah ; line feed
.STACK 4096 ; reserve 4096-byte stack
.DATA ; reserve storage for data
string1 BYTE “please input fifteen numbers: “,cr,Lf,0
string2 BYTE “the avg: “
avg BYTE 20 DUP(?)
outend BYTE cr,Lf,0
string3 BYTE “the result:”,cr,Lf,0
number BYTE 20 DUP(?)
array DWORD 20 DUP(?)
eltsit DWORD 20 DUP(?)
outValue BYTE 11 DUP(?),cr,Lf,0
.CODE ; start of main program code
_start:
output string1
lea ebx,array
whiles:
input number,20
atod number
mov [ebx],eax
inc Eltsit
cmp Eltsit,15
jge whileed
add ebx,4
jmp whiles
whileed:
mov eax,0
lea ebx,array
mov ecx,eltsit
forsum:
add eax,[ebx]
add ebx,4
loop forsum
cdq
idiv eltsit
lea ebx,array
mov ecx,eltsit
dtoa avg,eax
output string2
output outend
output string3
forupsum:
cmp [ebx],eax
jng endfor
dtoa outValue,[ebx]
output outValue
endfor:
add ebx,4
loop forupsum
INVOKE ExitProcess, 0 ; exit with return code 0
PUBLIC _start ; make entry point public
END ; end of source code
统计奇数偶数
从键盘输入一组数据,存入一维数组中(要求数组的数据占据一个字的空间),统计偶数和奇数并分别显示在屏幕上。
;Program:
;Author:
;Date:
.386
.MODEL FLAT
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
INCLUDE io.h ; header file for input/output
cr EQU 0dh ; carriage return character
Lf EQU 0ah ; line feed
.STACK 4096 ; reserve 4096-byte stack
.DATA ; reserve storage for data
string1 BYTE “please input numbers: (input # to end)”,cr,Lf,0
number WORD 20 DUP(?)
array WORD 100 DUP(?)
eltsit DWORD 20 DUP(?)
eltsit2 DWORD 20 DUP(?)
num DWORD 2
midnum DWORD 20 DUP(?)
outodd DWORD 20 DUP(?),0
string3 BYTE “is odd numbers:”
string4 BYTE “is even numbers:”,cr,Lf,0
endall BYTE cr,Lf,0
.CODE ; start of main program code
_start:
output string1
lea ebx,array
mov eltsit,0
;input number
whiles:
input number,11
cmp number,’#’
je whileed
atod number
mov [ebx],eax
inc eltsit
add ebx,4
jmp whiles
whileed:
lea ebx,array
output string3
;judge odd or even
forjudge1:
mov eax,[ebx]
mov edx,0
idiv num ;2
cmp edx,1
je odd1
jmp endodd1
odd1:
mov edx,[ebx]
dtoa midnum,edx
output midnum
endodd1:
add ebx,4
dec eltsit
inc eltsit2
cmp eltsit,0
je judgeed1
jmp forjudge1
judgeed1:
output endall
output string4
lea ebx,array
forjudge:
mov eax,[ebx]
mov edx,0
idiv num ;2
cmp edx,0
je odd
jmp endodd
odd:
mov edx,[ebx]
dtoa midnum,edx
output midnum
endodd:
add ebx,4
dec eltsit2
cmp eltsit2,0
je judgeed
jmp forjudge
judgeed:
output endall
INVOKE ExitProcess, 0 ; exit with return code 0
PUBLIC _start ; make entry point public
END ; end of source code
栈输入输出数据
从键盘输入一组数据,按照输入的倒叙显示这一组数组在屏幕上(利用堆栈完成)。
例如:输入 4,-30 ,5,6
显示:6,5,-30,4
;Program:
;Author:
;Date:
.386
.MODEL FLAT
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
INCLUDE io.h ; header file for input/output
cr EQU 0dh ; carriage return character
Lf EQU 0ah ; line feed
.STACK 4096 ; reserve 4096-byte stack
.DATA ; reserve storage for data
allnum WORD 20 dup (?),0
num1 WORD 20 dup (?),0
num2 WORD 20 dup (?),Lf,0
.CODE ; start of main program code
_start:
mov allnum,0
sectorA:
input num1,20
cmp num1,’#’
je sectorB
atod num1
push eax
inc allnum
jmp sectorA
sectorB:
pop ebx
dtoa num2,ebx
output num2
dec allnum
cmp allnum,0
je sectorBend
jmp sectorB
sectorBend:
INVOKE ExitProcess, 0 ; exit with return code 0
PUBLIC _start ; make entry point public
END ; end of source code
杨辉三角
从键盘输入一个数据,显示输出该数据行数的‘*’图形。
例如: 输入3,输出三行杨辉三角
;Program:
;Author:
;Date:
.386
.MODEL FLAT
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
INCLUDE io.h ; header file for input/output
cr EQU 0dh ; carriage return character
Lf EQU 0ah ; line feed
.STACK 4096 ; reserve 4096-byte stack
.DATA ; reserve storage for data
star BYTE 42,0 ;*
konge2 BYTE 32,0 ;
num1 BYTE 13 dup (?)
cont DWORD 4 dup (?)
endall BYTE cr,LF,0
.CODE ; start of main program code
_start:
input num1,13
atod num1
mov ecx,eax
dec eax
mov edx,1
loopp:
mov ebx,edx
mov cont,eax
sectorD:
cmp cont,0
je sectorB
output konge2
dec cont
jmp sectorD
sectorB:
cmp ebx,0
je sectorA
output star
dec ebx
jmp sectorB
sectorA:
output endall
dec eax
add edx,2
loop loopp
INVOKE ExitProcess, 0 ; exit with return code 0
PUBLIC _start ; make entry point public
END ; end of source code
