我的汇编语言程序模板

时间:2022-08-05 03:28:04

这里只有一些最基础的功能的代码,有时间继续完善。

本博客不适合学习及使用。

data segment
;input data segment code here
stack_store word 0fffh dup(0) ;reg stack
stack_top word 0000h ;reg stack
push_ax_store_ax word 0000h
push_ax_store_bx word 0000h
push_ax_store_cx word 0000h
push_ax_store_dx word 0000h
print_hex_store_var word 0000h
local_ax_store word 0000h
pause_str byte "press any key to continue...",10,13,'$'
endl_str byte 10,13,'$'
tab_str byte " ",'$'
ax_str byte "ax:",'$'
bx_str byte "bx:",'$'
cx_str byte "cx:",'$'
dx_str byte "dx:",'$'
data ends

stack segment
;input stack segment code here
stack ends

code segment
assume cs:code,ds:data,ss:stack
push_ax: ;push ax into reg stack
mov push_ax_store_ax,ax ;store flags
mov push_ax_store_bx,bx
lea bx,stack_store
add bx,stack_top ;get stack top
mov [bx],ax
add stack_top,0002h
mov ax,push_ax_store_ax ;reget flags
mov bx,push_ax_store_bx
ret
pop_ax: ;pop a value from reg stack to ax
mov push_ax_store_bx,bx ;store flags
lea bx,stack_store
sub stack_top,0002h
add bx,stack_top ;get stack top
mov ax,[bx]
mov bx,push_ax_store_bx ;reget flags
ret
store_reg: ;store reg into reg stack
mov local_ax_store,ax
call push_ax
mov ax,bx
call push_ax
mov ax,cx
call push_ax
mov ax,dx
call push_ax
mov ax,local_ax_store
ret
reget_reg: ;reget reg from reg stack
call pop_ax
mov dx,ax
call pop_ax
mov cx,ax
call pop_ax
mov bx,ax
call pop_ax
;mov ax,ax
ret
trans_dl_hex: ;trans late dl to hex char
cmp dl,09h
ja greater_than_09h
add dl,30h
ret
greater_than_09h:
sub dl,0ah
add dl,41h ;'a'=65=41h
ret
output_dl_char:
call store_reg
mov ah,02h
int 21h
call reget_reg
ret
print_hex_ax: ;output ax as a hex number
mov print_hex_store_var,ax
call store_reg
mov bx,print_hex_store_var
and bx,0f000h
mov cl,0ch
shr bx,cl
mov dl,bl
call trans_dl_hex
call output_dl_char
mov bx,print_hex_store_var
and bx,0f00h
mov cl,08h
shr bx,cl
mov dl,bl
call trans_dl_hex
call output_dl_char
mov bx,print_hex_store_var
and bx,00f0h
mov cl,04h
shr bx,cl
mov dl,bl
call trans_dl_hex
call output_dl_char
mov bx,print_hex_store_var
and bx,000fh
mov dl,bl
call trans_dl_hex
call output_dl_char
call reget_reg
ret
add_stack_ax: ;add ax elements into the stack
call store_reg
mov cl,02h
shl ax,cl
add stack_top,ax
call reget_reg
ret
print_str_dx: ;print a string at dx
call store_reg
mov ah,09h
int 21h
call reget_reg
ret
print_endl: ;print endl
call store_reg
lea dx,endl_str
call print_str_dx
call reget_reg
ret
pause_program: ;"press any key to continue..."
call store_reg
lea dx,pause_str
call print_str_dx
call getch_ax
call reget_reg
ret
getch_ax: ;get a char from key board (without enter) to ax
call store_reg
mov ah,07h
int 21h
mov ah,00h
mov local_ax_store,ax
call reget_reg
mov ax,local_ax_store
ret
debug_reg_val: ;output ax bx cx dx 's value
mov local_ax_store,ax ;store value
mov push_ax_store_bx,bx
mov push_ax_store_cx,cx
mov push_ax_store_dx,dx
call store_reg
lea dx,ax_str ;ax output
call print_str_dx
mov ax,local_ax_store
call print_hex_ax
lea dx,tab_str
call print_str_dx
lea dx,bx_str ;bx output
call print_str_dx
mov ax,push_ax_store_bx
call print_hex_ax
lea dx,tab_str
call print_str_dx
lea dx,cx_str ;cx output
call print_str_dx
mov ax,push_ax_store_cx
call print_hex_ax
lea dx,tab_str
call print_str_dx
lea dx,dx_str ;dx ouput
call print_str_dx
mov ax,push_ax_store_dx
call print_hex_ax
lea dx,tab_str
call print_str_dx
call print_endl ;endl output
call reget_reg
ret
start: ;program start here
mov ax,data
mov ds,ax
;input code segment code here

call pause_program ;pause end
mov ah,4ch
int 21h
code ends
end start

新增功能:十六进制数的输入

data segment
;input data segment code here
stack_store word 0fffh dup(0) ;reg stack
stack_top word 0000h ;reg stack
push_ax_store_ax word 0000h
push_ax_store_bx word 0000h
push_ax_store_cx word 0000h
push_ax_store_dx word 0000h
print_hex_store_var word 0000h
local_ax_store word 0000h
pause_str byte "press any key to continue...",10,13,'$'
endl_str byte 10,13,'$'
tab_str byte " ",'$'
ax_str byte "ax:",'$'
bx_str byte "bx:",'$'
cx_str byte "cx:",'$'
dx_str byte "dx:",'$'
scan_hex_store word 0000h
scan_hex_input byte 5,0,5 dup('$')
scan_hex_input_len word 0000h
clear_num_str byte 13,"[hex input]"," ",8,8,8,8,8,'$'
end_num_str byte "[you input]",'$'
data ends

stack segment
;input stack segment code here
stack ends

code segment
assume cs:code,ds:data,ss:stack
push_ax: ;push ax into reg stack
mov push_ax_store_ax,ax ;store flags
mov push_ax_store_bx,bx
lea bx,stack_store
add bx,stack_top ;get stack top
mov [bx],ax
add stack_top,0002h
mov ax,push_ax_store_ax ;reget flags
mov bx,push_ax_store_bx
ret
pop_ax: ;pop a value from reg stack to ax
mov push_ax_store_bx,bx ;store flags
lea bx,stack_store
sub stack_top,0002h
add bx,stack_top ;get stack top
mov ax,[bx]
mov bx,push_ax_store_bx ;reget flags
ret
store_reg: ;store reg into reg stack
mov local_ax_store,ax
call push_ax
mov ax,bx
call push_ax
mov ax,cx
call push_ax
mov ax,dx
call push_ax
mov ax,local_ax_store
ret
reget_reg: ;reget reg from reg stack
call pop_ax
mov dx,ax
call pop_ax
mov cx,ax
call pop_ax
mov bx,ax
call pop_ax
;mov ax,ax
ret
trans_dl_hex: ;trans late dl to hex char
cmp dl,09h
ja greater_than_09h
add dl,30h
ret
greater_than_09h:
sub dl,0ah
add dl,41h ;'a'=65=41h
ret
output_dl_char:
call store_reg
mov ah,02h
int 21h
call reget_reg
ret
print_hex_ax: ;output ax as a hex number
mov print_hex_store_var,ax
call store_reg
mov bx,print_hex_store_var
and bx,0f000h
mov cl,0ch
shr bx,cl
mov dl,bl
call trans_dl_hex
call output_dl_char
mov bx,print_hex_store_var
and bx,0f00h
mov cl,08h
shr bx,cl
mov dl,bl
call trans_dl_hex
call output_dl_char
mov bx,print_hex_store_var
and bx,00f0h
mov cl,04h
shr bx,cl
mov dl,bl
call trans_dl_hex
call output_dl_char
mov bx,print_hex_store_var
and bx,000fh
mov dl,bl
call trans_dl_hex
call output_dl_char
call reget_reg
ret
add_stack_ax: ;add ax elements into the stack
call store_reg
mov cl,02h
shl ax,cl
add stack_top,ax
call reget_reg
ret
print_str_dx: ;print a string at dx
call store_reg
mov ah,09h
int 21h
call reget_reg
ret
print_endl: ;print endl
call store_reg
lea dx,endl_str
call print_str_dx
call reget_reg
ret
pause_program: ;"press any key to continue..."
call store_reg
lea dx,pause_str
call print_str_dx
call getch_ax
call reget_reg
ret
getch_ax: ;get a char from key board (without enter) to ax
call store_reg
mov ah,07h
int 21h
mov ah,00h
mov local_ax_store,ax
call reget_reg
mov ax,local_ax_store
ret
debug_reg_val: ;output ax bx cx dx 's value
mov local_ax_store,ax ;store value
mov push_ax_store_bx,bx
mov push_ax_store_cx,cx
mov push_ax_store_dx,dx
call store_reg
lea dx,ax_str ;ax output
call print_str_dx
mov ax,local_ax_store
call print_hex_ax
lea dx,tab_str
call print_str_dx
lea dx,bx_str ;bx output
call print_str_dx
mov ax,push_ax_store_bx
call print_hex_ax
lea dx,tab_str
call print_str_dx
lea dx,cx_str ;cx output
call print_str_dx
mov ax,push_ax_store_cx
call print_hex_ax
lea dx,tab_str
call print_str_dx
lea dx,dx_str ;dx ouput
call print_str_dx
mov ax,push_ax_store_dx
call print_hex_ax
lea dx,tab_str
call print_str_dx
call print_endl ;endl output
call reget_reg
ret
retrans_hex_ax:
cmp ax,61h ;small
jl retrans_below_small
sub ax,20h ;-small
retrans_below_small:
cmp ax,41h ;'a'
jge retrans_geater_than_65h
sub ax,30h
ret
retrans_geater_than_65h:
sub ax,41h
add ax,0ah
ret
scan_hex_ax: ;read in a hex with four bits
call store_reg
scan_hex_ax_again:
lea dx,clear_num_str
call print_str_dx
mov bx,0000h
lea dx,scan_hex_input
mov bp,dx
mov ah,0ah
int 21h ;input string
mov dx,0000h
mov dl,[bp+01h] ;get len
cmp dx,04h
jnz scan_hex_ax_again
mov ah,00h ;first bit
mov al,[bp+02h]
call retrans_hex_ax
mov cl,04h
shl bx,cl
add bx,ax
mov ah,00h ;sec bit
mov al,[bp+03h]
call retrans_hex_ax
mov cl,04h
shl bx,cl
add bx,ax
mov ah,00h ;third bit
mov al,[bp+04h]
call retrans_hex_ax
mov cl,04h
shl bx,cl
add bx,ax
mov ah,00h ;forth bit
mov al,[bp+05h]
call retrans_hex_ax
mov cl,04h
shl bx,cl
add bx,ax
mov local_ax_store,bx ;store
call reget_reg
mov ax,local_ax_store
call print_endl
lea dx,end_num_str
call print_str_dx
call print_hex_ax
call print_endl
ret
start: ;program start here
mov ax,data
mov ds,ax
;input code segment code here
call scan_hex_ax

call pause_program ;pause end
mov ah,4ch
int 21h
code ends
end start