无法获得汇编语言代码的输出

问题描述

我是汇编语言的新手。我试图从用户中获取一个以 Enter 结尾的数字字符串,或者该字符串的长度达到20。执行该程序时,它没有显示任何错误,但也没有显示任何输出字符串超过20个字符的限制时也不会终止。

我的代码是:

.model small
.stack 100h
.data 
    var1 db 100 dup('$')
.code
main proc
mov ax,@data
mov dx,ax
mov si,offset var1

l1:
mov ah,1
int 21h
cmp al,20
je programend
mov [si],al
inc si
jmp l1

programend:
mov dx,offset var1
mov ah,9
int 21h
mov ah,4ch
int 21h

main endp
end main

 

解决方法

mov ax,@data
mov dx,ax

您想在此处初始化DS段寄存器,但错误地写了DX。老实的错字,但是您的代码将以这种方式破坏程序段前缀。

我正在尝试从ENTER键终止的用户那里获取一串数字,或者字符串的长度达到20

很明显,您需要循环执行此操作,并且您将需要进行2次测试才能确定何时停止!

  1. 测试AL中的字符是否为13
  2. 测试一个计数器(例如CX)是否达到20
  xor cx,cx           ; Empty counter
  mov si,offset var1
TheLoop:
  mov ah,01h          ; DOS.GetCharacter
  int 21h              ; -> AL
  cmp al,13
  je  programend
  mov [si],al
  inc si
  inc cx
  cmp cx,20
  jb  TheLoop
programend:

但是等等,任务不是说它必须是一串数字吗?您需要确保输入确实是数字。
数字“ 0”到“ 9”具有ASCII码48到57。

  xor cx,13
  je  programend
  cmp al,48
  jb  TheLoop          ; Not a number
  cmp al,57
  ja  TheLoop          ; Not a number
  mov [si],20
  jb  TheLoop
programend:

不使用单独的计数器,也不使用汇编程序将字符转换为代码的能力:

  mov si,"0"
  jb  TheLoop          ; Not a number
  cmp al,"9"
  ja  TheLoop          ; Not a number
  mov [si],al
  inc si
  cmp si,offset var1 + 20
  jb  TheLoop
programend:
,

请参见下面的代码中的注释。请记住不要将数据读取对ds:sidx:si混淆。有人可能会说ds:di在这里更合适。

main proc
    mov ax,@data
    mov ds,ax
    mov si,offset var1
    
    xor cl,cl ; set CX to zero
    
    l1:
        mov ah,1
        int 21h
        ; AL now contains the character read
        mov [si],al ; put the character into our variable
        inc si ; move up the memory cursor
        
        inc cl
        cmp cl,20
        jl l1
        ; if the number of iterations is less than 20,; do all of the above again
        ; otherwise proceed to program end
    
    programEnd:
        mov dx,offset var1
        mov ah,9
        int 21h
        ; at this point we have printed the string read
        
        mov ah,4ch
        int 21h
        ; and now we've terminated the program

main endp
end main

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...