主题:麻烦大家帮看看错在哪里
王爽〈〈汇编语言〉〉第十七章的一个例子,功能是接收键盘输入的字符串,并显示出来。本人新手,不知道错哪里了。
;---------------------------
data segment
db 128 dup(0)
ends
stack segment
dw 16 dup(0)
ends
code segment
start:
mov ax, data
mov ds, ax
mov ax, stack
mov ss, ax
mov sp, 32
mov si, 0
mov bx, 0
mov dh, 12
mov dl, 1
call getstr
mov ax, 4c00h
int 21h
;---------------------------
;(ah) == 0为入栈,此时(al)为入栈字符
;(ah) == 1为出栈,此时(al)为返回的字符
;(ah) == 2为显示,此时(dh)为行数,(dl)为列数
;ds:si指向字符栈空间
;
charstack:
jmp short charstart
table dw charpush, charpop, charshow ;三个子功能
top dw 0 ;栈顶
charstart:
push bx
push dx
push di
push es
cmp ah, 2 ;错误的参数
ja sret
mov bl, ah
mov bh, 0
add bx, bx
jmp word ptr table[bx] ;用了程序入口的直接定址表
charpush:
mov bx, top
mov [si][bx], al
inc top
jmp sret
charpop:
cmp top, 0
je sret ;栈空
dec top
mov bx, top
mov al, [si][bx]
jmp sret
charshow:
mov bx, 0b800h
mov es, bx
mov al, 160
mov ah, 0
mul dh
mov di, ax
add dl, dl
mov dh, 0
add di, dx
mov bx, 0
charshows:
cmp bx, top
jne noempty
mov byte ptr es:[di], ' '
jmp sret
noempty:
mov al, [si][bx]
mov es:[di], al
mov byte ptr es:[di + 2], ' '
inc bx
add di, 2
jmp charshows
sret:
pop es
pop di
pop dx
pop bx
ret
;---------------------------
getstr:
push ax
getstrs:
mov ah, 0
int 16h ;等待int9
cmp al, 20h
jb nochar ;ascii码小于0
mov ah, 0
call charstack ;字符入栈
mov ah, 2
call charstack ;显示栈中的字符
jmp getstrs
nochar:
cmp al, 0eh ;如果是退格键的扫描码
je backspace
cmp al, 1ch ;如果是回车键的扫描码
je enter
jmp getstrs ;显示栈中字符
backspace:
mov ah, 1
call charstack ;出栈
mov ah, 2
call charstack
jmp getstrs
enter:
mov al, 0
mov ah, 0
call charstack ;0入栈
mov ah, 2
call charstack
pop ax
ret
ends
end start
;---------------------------
data segment
db 128 dup(0)
ends
stack segment
dw 16 dup(0)
ends
code segment
start:
mov ax, data
mov ds, ax
mov ax, stack
mov ss, ax
mov sp, 32
mov si, 0
mov bx, 0
mov dh, 12
mov dl, 1
call getstr
mov ax, 4c00h
int 21h
;---------------------------
;(ah) == 0为入栈,此时(al)为入栈字符
;(ah) == 1为出栈,此时(al)为返回的字符
;(ah) == 2为显示,此时(dh)为行数,(dl)为列数
;ds:si指向字符栈空间
;
charstack:
jmp short charstart
table dw charpush, charpop, charshow ;三个子功能
top dw 0 ;栈顶
charstart:
push bx
push dx
push di
push es
cmp ah, 2 ;错误的参数
ja sret
mov bl, ah
mov bh, 0
add bx, bx
jmp word ptr table[bx] ;用了程序入口的直接定址表
charpush:
mov bx, top
mov [si][bx], al
inc top
jmp sret
charpop:
cmp top, 0
je sret ;栈空
dec top
mov bx, top
mov al, [si][bx]
jmp sret
charshow:
mov bx, 0b800h
mov es, bx
mov al, 160
mov ah, 0
mul dh
mov di, ax
add dl, dl
mov dh, 0
add di, dx
mov bx, 0
charshows:
cmp bx, top
jne noempty
mov byte ptr es:[di], ' '
jmp sret
noempty:
mov al, [si][bx]
mov es:[di], al
mov byte ptr es:[di + 2], ' '
inc bx
add di, 2
jmp charshows
sret:
pop es
pop di
pop dx
pop bx
ret
;---------------------------
getstr:
push ax
getstrs:
mov ah, 0
int 16h ;等待int9
cmp al, 20h
jb nochar ;ascii码小于0
mov ah, 0
call charstack ;字符入栈
mov ah, 2
call charstack ;显示栈中的字符
jmp getstrs
nochar:
cmp al, 0eh ;如果是退格键的扫描码
je backspace
cmp al, 1ch ;如果是回车键的扫描码
je enter
jmp getstrs ;显示栈中字符
backspace:
mov ah, 1
call charstack ;出栈
mov ah, 2
call charstack
jmp getstrs
enter:
mov al, 0
mov ah, 0
call charstack ;0入栈
mov ah, 2
call charstack
pop ax
ret
ends
end start