当前位置: 代码迷 >> 汇编语言 >> 求帮忙诠释一个汇编程序代码
  详细解决方案

求帮忙诠释一个汇编程序代码

热度:292   发布时间:2016-05-02 04:26:02.0
求帮忙注释一个汇编程序代码
len equ 10
.model small
.stack 100h

.data
input_msg db 0dh,0ah,'Input Number '              
Num_no db '00 (0-255):$'                          
no_str db 'Nothing, NO ODD NUMBER !','$'
buf db 4,0,4 dup (0)
input_data db 10 dup (0)
result_str db 0dh,0ah,'The minimum odd number is $'
first_lo dw 0
first_hi dw 0

.code
start:
MOV AX,@DATA
MOV DS,AX
mov es,ax
cld
call cls
mov cx,10
mov di,offset input_data

s10:
push cx
push di
mov buf + 1,0 ;clear 
inc Num_no + 1
cmp Num_no + 1,'9'
jbe s20
mov Num_no + 1,'0'
inc Num_no

s20:
lea dx,input_msg
call get_input
cmp first_hi,0
jnz s20
mov ax,first_lo
cmp ax,255
ja s20

pop di
stosb ;save result
pop cx
loop s10

mov ah,09
mov dx,offset result_str
int 21h

LEA si,input_data
mov bl,255
MOV CX,Len
xor dx,dx

next1:
lodsb
test al,1
jz next2
cmp al,bl
ja next2
inc dx
xchg al,bl
next2:
loop next1
cmp bl,255
jz next3
mov al,bl
call BCD_output
next3:
cmp dx,0
jnz quit
mov dx,offset no_str
mov ah,9
int 21h
quit:
MOV AH,4CH
INT 21H
;-------------------------
; output Al (0-255) BCD output
BCD_output:
push dx
push cx
xor cx,cx
bcd5:
add al,0
aam
bcd7:
push ax
inc cx
or ah,ah
jz bcd20
cmp ah,0Ah
jb bcd10
mov al,ah
xor ah,ah
jmp short bcd5
bcd10:
mov al,ah
xor ah,ah
jmp short bcd7
bcd20:
pop dx
or dl,'0'
mov ah,2
int 21h
loop bcd20
pop cx
pop dx
ret
;-------------------------
cls:
mov ah,0fh
int 10h
mov ah,0
int 10h
ret
;-------------------------
get_input:
xor ax,ax
mov first_lo,ax
mov first_hi,ax
mov ah,9
int 21h
mov buf+1,0 ;clear
lea dx,buf
mov ah,10
int 21h
mov cl,buf+1
xor ch,ch
xor bx,bx ; clear
xor dx,dx ;clear
lea si,buf + 2 ;offset
call dtoh
jc get_input
ret
;-------------------------
;si = string offset
;calc dec to hi-lo buffer
dtoh:
push ax
push bx
push cx
push dx
push si
push bp
lea si,buf + 2
xor dx,dx
mov bx,10
d10:
lodsb
cmp al,0dh
jz d20
jz d20
cmp al,'0'
jae d15

d12:
stc
jmp short d20

d15:
cmp al,'9'
ja d12
sub al,30h
cbw
mov bp,ax ;store to bp
mov ax,first_hi ;get first_hi
mul bx ;x 10
mov first_hi,ax ;store to key hi
mov ax,bp ;restore from bp
xchg ax,first_lo ;get key lo
mul bx ;x 10
xchg ax,first_lo ;store to key lo
add first_lo,ax ;add low byte to key lo
adc first_hi,dx ;add to key hi
jnc d10
jmp short d12
d20:
pop bp
pop si
pop dx
pop cx
pop bx
pop ax
ret
;--------------------------------
END START



感激不尽
------解决思路----------------------
len equ 10
.model small
.stack 100h

.data
input_msg db 0dh,0ah,'Input Number '              
Num_no db '00 (0-255):$'                          
no_str db 'Nothing, NO ODD NUMBER !','$'
buf db 4,0,4 dup (0)
input_data db 10 dup (0)
result_str db 0dh,0ah,'The minimum odd number is $'
first_lo dw 0
first_hi dw 0

.code
start:
MOV AX,DATA
MOV DS,AX
mov es,ax
cld
call cls                                                                                 ; 清屏
mov cx,10                                                                            ; CX(外循环的循环控制变量) <- 10
mov di,offset input_data                                                   ; 将DI指向input_data

s10:
push cx
push di
mov buf + 1,0                                                                     ; buf+1 <- 0
inc Num_no + 1                                                                ; (Num_no+1)++

cmp Num_no + 1,'9'                                                         ; 比较Num_no+1与'9'的大小
jbe s20                                                                                ; 若小于等于,则跳转到s20

mov Num_no + 1,'0'                                                          ; Num_no+1 <- '0'
inc Num_no                                                                        ; Num_no++

s20:
lea dx,input_msg                                                              ; 将DX指向字符串input_msg
call get_input                                                                     ; 读取用户的输入,将其转换成十进制数字
cmp first_hi,0                                                                     ; 比较first_hi与00是否相等
jnz s20                                                                                 ; 若不等,则跳转到s20,内循环

mov ax,first_lo                                                                    ; AX <- first_lo
cmp ax,255                                                                         ; 比较AX与255的大小
ja s20                                                                                   ; 若大于,则跳转到s20,内循环

pop di
stosb                                                                                    ; ES:[DI++] <- AL
pop cx
loop s10                                                                               ; 跳转到s10,外循环

mov ah,09
mov dx,offset result_str                                                     ; 将DS:DX指向字符串result_str
int 21h                                                                                   ; WRITE STRING TO STANDARD OUTPUT

LEA si,input_data                                                               ; 将DS:SI指向input_data
mov bl,255                                                                           ; BL <- 255
MOV CX,Len                                                                        ; CX <- Len(10)
xor dx,dx                                                                               ; DX = 0

;  循环,
next1:
lodsb                                                                                    ; AL <- DS:[SI++]
test al,1                                                                                ; 测试AL中的bit 0是否为0
jz next2                                                                                 ; 若为0,则为偶数

; 奇数
cmp al,bl                                                                             ; 比较AL与BL的大小
ja next2                                                                               ; 若大于,则跳转到next2

inc dx                                                                                   ; DX++
xchg al,bl                                                                            ; 交换AL与BL的值

next2:
loop next1                                                                         ; 跳转到next1,循环

cmp bl,255                                                                       ; 比较BL与255是否相等
jz next3                                                                              ; 若相等,则跳转到next3 

mov al,bl                                                                          ; AL <- BL(最小的奇数)
call BCD_output                                                             ; 将AL中的数值转换成数值字符串输出

next3:
cmp dx,0                                                                          ; 比较DX与0是否相等
jnz quit                                                                              ; 若不等,则必定有奇数,跳转到quit

; 没有奇数
mov dx,offset no_str                                                       ; 将DX指向字符串no_str
mov ah,9
int 21h                                                                                ; 输出字符串no_str

quit:
MOV AH,4CH
INT 21H                                                                             ; 程序退出                               

;-------------------------
; output Al (0-255) BCD output
; 将AL中的数值转换成数值字符串输出
BCD_output:
push dx
push cx
xor cx,cx                                                                         ; CX = 0

bcd5:
add al,0
aam                                                                               ; AL为AX除以10的余数,AH为AX除以10的商

bcd7:
push ax                                                                        ; 将AX进栈,把AL(余数)保存到栈上
inc cx                                                                            ;  CX(保存到栈上的余数的计数器)++
or ah,ah
jz bcd20                                                                       ; 若AH(商)为0,则跳转到bcd20

cmp ah,0Ah                                                                ; 比较AH(商)与0AH大小
jb bcd10                                                                      ; 若小于,则跳转到bcd10

mov al,ah                                                                    ; AL <- AH
xor ah,ah                                                                     ; AH = 0
jmp short bcd5                                                          ; 跳转到bcd5,循环

bcd10:
mov al,ah                                                                    ; AL <- AH
xor ah,ah                                                                     ; AH = 0
jmp short bcd7                                                          ; 跳转到bcd7

bcd20:
pop dx
or dl,'0'                                                                        ; DL = DL 
------解决思路----------------------
 30H,得到DL为数字对应的ASCII码
mov ah,2
int 21h                                                                         ; WRITE CHARACTER TO STANDARD OUTPUT

loop bcd20                                                                ; 跳转到bcd20

pop cx
pop dx
ret
;-------------------------
cls:
mov ah,0fh
int 10h                                                                           ; GET CURRENT VIDEO MODE,输出AL显示模式

mov ah,0
int 10h                                                                           ; SET VIDEO MODE,设置AL为显示模式,通过设置显示模式,清屏
ret

------解决思路----------------------
;-------------------------
;  读取用户的输入,将其转换成十进制数字
get_input:
xor ax,ax                                                                        ; AX = 0
mov first_lo,ax                                                             ; first_lo <- ax 
mov first_hi,ax                                                             ; first_hi <- ax

mov ah,9
int 21h                                                                           ; 输出input_msg所指向的字符串

mov buf+1,0                                                                 ; bug+1 <- 0

lea dx,buf                                                                      ; 将DS:DX指向buf
mov ah,10
int 21h                                                                           ; BUFFERED INPUT

mov cl,buf+1                                                                ; CL <- buf+1
xor ch,ch                                                                       ; CH = 0
xor bx,bx                                                                       ; BX = 0
xor dx,dx                                                                       ; DX = 0
lea si,buf + 2                                                               ; 将SI指向buf+2
call dtoh                                                                       ; 将输入字符串转换成十进制数
jc get_input                                                                 ; 若出错,则跳转到函数的开始位置,继续
ret
;-------------------------
;si = string offset
;calc dec to hi-lo buffer
; 将输入字符串转换成十进制数
dtoh:
push ax
push bx
push cx
push dx
push si
push bp

lea si,buf + 2                                                               ; 将SI指向buf+2
xor dx,dx                                                                       ; DX = 0
mov bx,10                                                                    ; BX <- 10(10进制)

d10:
lodsb                                                                            ; AL <- DS:[SI++]
cmp al,0dh                                                                  ; 比较AL与0DH(回车键)是否相等
jz d20                                                                            ; 若相等,则跳转到d20
jz d20                                                                            ; ???,重复
cmp al,'0'                                                                      ; 比较AL与‘0’的大小
jae d15                                                                         ; 若大于等于,则跳转到d15

d12:
stc                                                                                 ; CF = 1,表示出错
jmp short d20                                                             ; 跳转到d20

d15:
cmp al,'9'                                                                     ; 比较AL与‘9’的大小
ja d12                                                                          ; 若大于,则跳转到d12

; 将first_hi:first_lo乘以10,加上低位数
sub al,30h                                                                  ; AL -= 30H('0')
cbw                                                                              ; 将AL的值扩展到AX
mov bp,ax                                                                   ; BP <- AX
mov ax,first_hi                                                           ; AX <- first_hi
mul bx                                                                         ; AX * BX(10) = DX:AX
mov first_hi,ax                                                           ; first_hi <- AX
mov ax,bp                                                                   ; AX <- BP
xchg ax,first_lo                                                           ; 交换ax和first_lo的值
mul bx                                                                          ; AX * BX(10) = DX:AX
xchg ax,first_lo                                                           ; 交换ax和first_lo的值
add first_lo,ax                                                             ;  first_lo += AX
adc first_hi,dx                                                             ; first_hi += DX
jnc d10                                                                        ; 若未进位,则跳转到d10
jmp short d12                                                           ; 否则,跳转到d12

d20:
pop bp
pop si
pop dx
pop cx
pop bx
pop ax
ret
;--------------------------------
END START