你好,我只是在学习汇编,所以我还不太了解。
我必须编写一个程序,让用户输入各种字母数字等的行。该程序应该计算输入中有多少个数字并打印出计数器。
这是我的代码:
.model small
.stack 100h
.data
buffer db 100, ?, 100 dup (0)
count db 0
.code
start:
mov ax, @data
mov ds, ax
mov dx, offset buffer
mov ah, 0Ah
int 21h
mov ah, buffer
xor si, si
xor cx, cx
.loop:
.notdigit:
mov dl, buffer[si]
inc Si
cmp dl, 0
jz .end
cmp dl, '0'
jb .notdigit
cmp dl, '9'
ja .notdigit
inc count
jmp .loop
.end:
; count contains the digit count
mov dl, count
mov ah, 2h
int 21h
我没有收到任何错误,但是运行该程序时它确实无法正常工作。
这是怎么了?我该如何更改?
答案 0 :(得分:1)
buffer db 100, ?, 100 dup (0)
这是DOS函数0Ah使用的输入缓冲区的正确定义,但是稍后,当您要遍历实际的输入字符串时,您需要跳过前2个字节,因为这些不是实际输入文字的一部分!
您可以将xor si, si
更改为mov si, 2
。
cmp dl, 0 jz .end
DOS为您提供的输入以回车符(ASCII 13)终止,因此无法测试零。
下面的代码使用AL
而不是DL
,因为生成的汇编代码会短一些。
存在替代解决方案,但这是最接近您的解决方案:
mov si, 2
.loop:
mov al, buffer[si]
inc si
cmp al, 13
je .end
cmp al, '0'
jb .loop ;notdigit
cmp al, '9'
ja .loop ;notdigit
inc count
jmp .loop
.end:
mov dl, count mov ah, 2h int 21h
此DOS函数在DL
中需要一个字符。您的 count 变量只是一个数字,很可能是一个很小的数字!
通过添加48,您可以轻松地将0到9的小数字转换为它们各自的字符。
mov dl, count
add dl, '0' ;ASCII code for '0' is 48
mov ah, 02h
int 21h
答案 1 :(得分:0)
此代码的重点是演示几种方法,可以用来返回代表字符串中数字字符数量的十进制整数,这是OP中缺少的部分。事后通常使用某种转换算法,但是我认为在解析字符串时如何完成转换可能很有趣。
由于DOS应用程序的许多示例本质上仅是平面内存模型,我省去了诸如节(.data,.const,.text)之类的多余内容,并使用 RET 返回命令提示,因为我们并不真正关心返回码。我很少像 COM 文件那样保留空间,这只会使应用程序肿。
使用 NASM name.asm -oname.com 版本2.11.08组装
使用DOSBOX 0.74测试
WRITE_STR equ 9
READ_STR equ 0xc0a ; Buffered input with initial flush
DOS equ 33
org 100H ; DOS 6.22 COM file
; ----------------------------------------------------------------------------
Begin:
; Every program should have some sort of prompting so operator has an idea
; of what is expected.
mov dx, Prompt
mov ah, WRITE_STR
int DOS
; Not absolutely required, but AH = 0CH & AL = 0AH accepts buffered input
; but assures there aren't any straggling characters as buffer pointed to by
; DX is flushed first.
mov dx, Users_Entry
mov ax, READ_STR
int DOS
; Setup pointer to string, count of characters entered and initial count
; of digit characters in string
mov si, dx ; Point to input buffer
inc si ; Bounce over maximum characters
xor ax, ax
push ax
lodsb ; Read # of characters entered
mov cx, ax ; Move to counter register for loop
; Of the four possible outcomes, each message must be preceded with
; double return & tab.
mov dx, PreAmb
mov ah, WRITE_STR
int DOS
; The reason AX is being used because a packed BCD value is going to be
; created on the fly, but for this to work DAA must be used and it only
; works on the accumulator.
pop ax ; Restore initial digits count
mov dx, Err00 ; By default, assume nothing was entered.
jcxz Finished ; Branch in buffer is empty
.NextChr:
mov bl, [si] ; Read first or next character
inc si ; Bump pointer
cmp bl, '0'
jb .notNum
cmp bl, '9'
ja .notNum
add al, 1
daa ; Bump counter and decimal adjust
.notNum:
loop .NextChr
mov dx, Msg01 ; Assume a single digit character.
cmp al, 1
jz Finished
mov dx, Msg00
or al, al
jz Finished ; No digit characters found
; Now we are going to replace the word "No" with the BCD value in AX
cld
mov di, dx
push ax
mov dx, di ; Needed to ultimately display string
; There is a good change the value is < 10, so initially we are going
; to convert to space.
shr ax, 4 ; Shift tens into low nibble
or al, ' '
cmp al, ' '
jz $ + 4 ; I
or al, 10000b ; Convert ot digit 1 - 9
stosb ; Write to destination string
pop ax
and ax, 1111b ; Mask out high nibble (tens)
or al, '0'
stosb ; Write units digit.
; Now the appropriate final message can be displayed appending modified
; PreAmb to it.
Finished:
call Show ; Display desired result string
mov dx, PreAmb + 1 ; Do one less line-feed
mov di, dx
mov byte [di+2], '$' ; Don't want to do TAB
Show:
mov ah, WRITE_STR
int DOS
ret ; Return to DOS or maybe DEBUG
; NOTE: alignment is for convenience sake so things can be identified a little
; more readily using hex dumps in DEBUG.
align 16
; ----------------------------------------------------------------------------
Prompt: db 27, '[2J', 10 ; VT100 emulation clears screen and sets
; cursor to 0:0
db ' Please enter alphanumeric string (max 48 chars)'
db 10, 10, ' --> $'
PreAmb: db 10, 10, 9, '$'
Err00: db '--> FAILED <-- NULL string$'
Msg00: db 'No digit characters were found$'
Msg01: db 'Only a single digit character found$'
align 8
Users_Entry: db 49 ; NOTE: change prompting text too.