当前位置:网站首页>汇编基础代码示例
汇编基础代码示例
2022-04-23 05:51:00 【Round moon】
字符串传递
data segment
src db "hello!"
data ends
extra segment
new db 6 dup("0")
extra ends
code segment
assume cs:code,ds:data,es:extra
start:
mov ax,data
mov ds,ax
mov ax,extra
mov es,ax
lea si,src
lea di,new
mov cx,6
cld
rep movsb
code ends
end start
输出单个符号
表示输出单个ascii码的值
prog1 segment
assume cs:prog1
start:
mov dl,'?';ASCII码
mov ah,2
int 21h
mov ax,4c00h
int 21h
prog1 ends
end start
求数字平方值
data segment
table db 0,1,4,9,16,25,36,49,64,81
buf db "Please input a number(0~9):",0DH,0AH,"$"
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov dx,offset buf
mov ah,9h
int 21h
mov ah,1
int 21h
and al,0fh
mov bx,offset table
mov ah,0
add bx,ax
mov al,[bx]
mov ax,4c00h
int 21h
code ends
end start
两数之和
data segment
a1 db 56h,78h
a2 db 4fh,9ah
sum db 3 dup(0)
data ends
code segment
assume cs:code,ds:data
begin:
mov ax,data
mov ds,ax
mov bx,0
clc
mov al,a1[bx]
adc al,a2[bx]
mov sum[bx],al
inc bx
mov al,a1[bx]
adc al,a2[bx]
mov sum[bx],al
jnc STOP
inc bx
mov al,1
mov sum[bx],al
STOP:
mov ax,4c00h
int 21h
code ends
end begin
打印成绩单
al存放16进制的成绩
输出对应的成绩
cmp al,60
jb fail
cmp al,85
jae good
mov al,"P"
jmp print
fail:
mov al,'F'
jmp print
good:
mov al,"G"
jmp print
print:
mov dl,al
mov ah,2
int 21h
mov ax,4c00h
int 21h
字符统计
统计数字 大写字母和其他字母的数量
data segment
buf db "PRINT","abc",35h,52h,30h,08h
count equ $-buf
num db 3 dup(0)
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov ch,count
mov bx,0
mov dx,0
loop1:
mov ah,buf[bx]
cmp ah,30h
jl next
cmp ah,39h
jg abc
inc dh
jmp next
abc:
cmp ah,41h
jl next
cmp ah,5ah
jg next
inc dl
next:
inc bx
dec ch
jnz loop1
mov num,dh
mov num+1,dl
mov ah,count
sub ah,dh
sub ah,dl
mov num+2,ah
mov ax,4c00h
int 21h
code ends
end start
寻找最大值
data segment
save dw 1234h,3200h,4832h,5600h
count equ ($-save)/2
max dw ?
data ends
code segment
assume code:cs,data:ds
start:
mov ax,data
mov ds,ax
mov bx,0
mov cx,count
dec cx
mov dx,save[0]
mov max,dx
again:
add bx,2
mov dx,save[bx]
cmp max,dx
jg next
mov max,dx
next:
loop again
code ends
end start
BCD加法
data segment
A DB 44h,33h,22h,11h
B DB 88h,77h,66h,55h
ans 5 dup(0)
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov bx,0
mov cx,4
again:
mov al,A[bx]
add al,B[bx]
daa
mov ans[bx],al
jnc NEXT
add A[bx+1],1
NEXT:
inc bx
loop again
code ends
end start
冒泡排序
data segment
list dw 12,7,19,8,24
count equ ($-list)/2
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov cx,count-1
loop1:
mov dx,cx
mov bx,0
loop2:
mov ax,list[bx]
cmp ax,list[bx+2]
JGE NO_CHANGE
xchg ax,list[bx+2]
mov list[bx],ax
NO_CHANGE:
add bx,2
loop loop2
mov cx,dx
loop loop1
mov ax,4c00h
int 21h
code ends
end start
输出数字
AL位62H对应十进制数字98
Bin_ascii proc near
mov ah,0
mov bl,10
div bl
xchg ah,al
add ax,3030h
mov cx,ax
mov dl,ch
mov ah,2
int 21h
mov dl,cl
int 21h
ret
Bin_ascii endp
存储数字
输出十进制数字,存入bx中
DEC_BIN proc near
mov bx,0
get_char:
mov ah,1
int 21h
cmp al,0dh
je exit
sub al,30h
jl exit
cmp al,9
jg exit
cbw
xchg ax,bx
mov cx,10
mul cx
xchg ax,bx
add bx,ax
jmp get_char
exit:
ret
DEC_BIN endp
输出16进制
将bx中的数据以十六进制输出
BIN_HEX proc near
mov cx,4
begin:
mov dl,bh
shr dl,4
add dl,30h
cmp dl,'9'
jl Display
sub dl,'9'+1
add dl,"A"
Display:
mov ah,2
int 21h
shl bx,4
loop begin
BIN_HEX endp
十六进制转BCD
这里需要注意的是,字节除数的时候,DX是做高八位的
bin_bcd proc near
mov ax,04D2h
cmp ax,9999
jbe tran
jmp exit
tran:
mov dx,0
mov cx,1000
div cx
xchg ax,dx
shl bx,4
add bx,dx
mov dx,0
mov cx,100
div cx
xchg ax,dx
shl bx,4
add bx,dx
mov dx,0
mov dx,0
mov cx,10
div cx
xchg ax,dx
shl bx,4
add bx,dx
mov dx,0
mov cx,1
div cx
xchg ax,dx
shl bx,4
add bx,dx
mov dx,0
exit:
ret
bin_bcd endp
求BCD和
使用过程调用的方法
data segment
num1 db 44h,33h,22h,11h
num2 db 88h,77h,66h,55h
sum db 5 dup(0)
data ends
stack segment stack
dw 50 dup()
top label word
stack ends
code segment
main proc far
assume cs:code,ds:data,ss:stack
start:
mov ax,stack
mov ss,ax
mov sp, offset top
push ds
push 0
mov ax,data
mov ds,ax
mov es,ax
lea si,num1
lea bx,num2
lea di,sum
clc
cld
mov ah,0
mov cx,4
loop1:
call add_b
loop loop1
adc ah,0
mov al,ah
stosb
ret
main endp
add_b proc near
lodsb
adc al,[bx]
daa
stosb
inc bx
ret
add_b endp
code ends
end main
斐波那契
data segment
Fibonacci db 1,1,5 dup()
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov cx,5
mov dh,0
mov dl,1
Loop1:
mov bx,0
mov bl,dh
mov ah,Fibonacci[bx]
mov bl,dl
mov al,Fibonacci[bx]
add ah,al
inc dh
inc dl
inc bx
mov Fibonacci[bx],ah
loop Loop1
code ends
end start
版权声明
本文为[Round moon]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_35339563/article/details/121075198
边栏推荐
猜你喜欢

PM2 deploy nuxt project

Initialization of classes and objects (constructors and destructors)

Programmers can also write novels
![[UDS unified diagnostic service] II. Network layer protocol (1) - overview and functions of network layer](/img/39/30bb897ff4467105de08c8c1c737ab.png)
[UDS unified diagnostic service] II. Network layer protocol (1) - overview and functions of network layer

Installation of GCC, G + +, GDB

clion安装教程

Swagger2 generates API documents

【UDS统一诊断服务】二、网络层协议(2)— 数据传输规则(单帧与多帧)
[ThreadX] h743zi + lan8720 + ThreadX + netx duo transplantation

cuda工程更换环境(电脑)后遇到的一系列编译问题
随机推荐
C语言实现memcpy、memset、strcpy、strncpy、strcmp、strncmp、strlen
Cross domain issues - allow origin header contains multiple values but only one is allowed
带默认模板实参的类模板与模板模板形参的匹配
ArcGIS license错误-15解决方法
gcc ,g++,gdb的安装
CUDA环境安装
Graduation project, viewing screenshots of epidemic psychological counseling system
类和对象的初始化(构造函数与析构函数)
[UDS unified diagnosis service] IV. typical diagnosis service (3) - read fault information function unit (storage data transmission function unit)
拷贝构造函数
Call procedure of function
Wechat applet request encapsulation
clion安装教程
[UDS unified diagnostic service] II. Network layer protocol (2) - data transmission rules (single frame and multi frame)
代理服务器
Static member
逻辑回归原理及代码实现
pyppeteer爬虫
如何读文献
Camera calibration: key point method vs direct method