21,458
社区成员
发帖
与我相关
我的任务
分享
E:\lbc\mycode\16bitasm>lbc_demo.exe
n1=?(0<n<65536)1
n2=?(0<n<65536)1
n1*n2=1
E:\lbc\mycode\16bitasm>lbc_demo.exe
n1=?(0<n<65536)2
n2=?(0<n<65536)3
n1*n2=6
E:\lbc\mycode\16bitasm>lbc_demo.exe
n1=?(0<n<65536)14
n2=?(0<n<65536)15
n1*n2=210
E:\lbc\mycode\16bitasm>lbc_demo.exe
n1=?(0<n<65536)30000
n2=?(0<n<65536)30000
n1*n2=900000000
E:\lbc\mycode\16bitasm>lbc_demo.exe
n1=?(0<n<65536)60000
n2=?(0<n<65536)50000
n1*n2=3000000000
E:\lbc\mycode\16bitasm>lbc_demo.exe
n1=?(0<n<65536)999
n2=?(0<n<65536)999
n1*n2=998001
TITLE lbc_test
.model small
stack segment
db 100 dup(?)
stack ends
data segment
n1 dw ?
n2 dw ?
result dd ?
in_buff db 16,0
db 16 dup (?)
prompt1 db 0dh,0ah,"n1=?(0<n<65536)$"
prompt2 db 0dh,0ah,"n2=?(0<n<65536)$"
prompt3 db 0dh,0ah,"n1*n2="
out_buff db 32 dup (?)
data ends
code segment
assume cs:code,ds:data,ss:stack
start:
mov ax,stack
mov ss,ax
mov ax,data
mov ds,ax
mov dx, offset prompt1
mov ah, 9h
int 21h
mov si, offset in_buff
call input_num
mov n1,ax
mov dx, offset prompt2
mov ah, 9h
int 21h
mov si, offset in_buff
call input_num
mov n2,ax
mov ax,n1
mov bx,n2
mul bx
mov word ptr result, ax
mov word ptr result+2,dx
mov di, offset out_buff
call output_num
mov dx, offset prompt3
mov ah, 9h
int 21h
mov ax,4c00h ; terminate program
int 21h
;input a string from standard input device
;function:
; input a string from standard input device, then convert string to a 16bit number
;input parameter:
; dx: input buff, the first char [dx], the buff max length
;output parameter
; ax: return the value of input
input_num proc
mov dx,si
mov ah,0ah
int 21h
add si,2 ;skip the first 2 char and point to the first input char
xor ax,ax ;ax return value
xor cx,cx ;clean cx
mov bx,10
loop_read_char:
mov cl,byte ptr [si] ; get a char from in_buff
sub cl,'0'
cmp cl,9
ja input_exit ;if the char <'0' or >'9', then jump out loop
mul bx
add ax,cx
inc si
jmp loop_read_char
input_exit:
ret
input_num endp
;conver a number result to output_buffer and fill '$' after number string
;function:
; conver 32 bit integer result to string
;input parameter:
; none, always convert varible result
;output parameter
; di: The address of output buffer
output_num proc
push di ;di is the address of out_buff
mov bx,10
convert_high:
xor dx,dx
mov ax,word ptr [result+2]
div bx
mov word ptr [result+2],ax
convert_low:
mov ax,word ptr [result]
div bx
mov word ptr [result],ax
add dl,'0' ;save (result % 10)+'0' to out_buffer
mov byte ptr [di],dl
inc di ;di point next position
cmp_r:
cmp word ptr [result+2],0
jnz convert_high
mov dx,0
cmp word ptr [result],0
jnz convert_low
inv_string: ;swap string head and string tail
pop si ;now si is the address of out_buff
dec di ;the di point to the last char
mov bx,di
inv_loop:
mov al,[si] ;swap char
mov ah,[di]
mov [di],al
mov [si],ah
inc si
dec di
cmp_head_tail:
cmp si,di
jb inv_loop
mov byte ptr [bx+1], '$' ; For dos ah=09, string output, the terminal char must be '$'
ret
output_num endp
code ends
end start