【问题标题】:Can't get output of assembly language code无法获得汇编语言代码的输出
【发布时间】:2020-08-16 16:50:15
【问题描述】:

我是汇编语言的新手。我试图从用户那里获取一串数字,该字符串由 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

 

【问题讨论】:

    标签: assembly dos x86-16 masm


    【解决方案1】:
    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, cx           ; Empty counter
      mov si, offset var1
    TheLoop:
      mov ah, 01h          ; DOS.GetCharacter
      int 21h              ; -> AL
      cmp al, 13
      je  programend
      cmp al, 48
      jb  TheLoop          ; Not a number
      cmp al, 57
      ja  TheLoop          ; Not a number
      mov [si], al
      inc si
      inc cx
      cmp cx, 20
      jb  TheLoop
    programend:
    

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

      mov si, offset var1
    TheLoop:
      mov ah, 01h          ; DOS.GetCharacter
      int 21h              ; -> AL
      cmp al, 13
      je  programend
      cmp al, "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:
    

    【讨论】:

      【解决方案2】:

      请参阅下面代码中的 cmets。请记住不要将数据读取对 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
      

      【讨论】:

      • xor cl, cl ; set CX to zero我不认为这个评论是真的!
      猜你喜欢
      • 1970-01-01
      • 2014-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多