簡體   English   中英

嘗試將用戶輸入作為循環計數器 [NASM 匯編語言]

[英]Trying to take user input as loop counter [NASM Assembly language]

我試圖運行一個循環,該循環在用戶輸入的值上運行。

數據段:

  BITS 32
 section .data                          
        msg db 'Enter the number of lines '
        len equ $-msg
        hello db 'Hello World ! '
        lenhello equ $-hello
        cr db 10               

.bss 段

section .bss    
        num resb 5

.txt 段

section .text          ;Code Segment
       global _start

_start:                ;User prompt
       mov eax, 4
       mov ebx, 1
       mov ecx, msg
       mov edx, len
       int 80h

       mov eax, 3
       mov ebx, 2
       mov ecx, num
       mov edx, 5
       int 80h

       mov ecx, num ; i think the issue is here
li: 
       call newline
       push ecx
       mov eax, 4
       mov ebx, 1 
       mov ecx, hello
       mov edx, lenhello
       int 80h
       pop ecx
loop li
       mov eax, 1
       mov ebx, 0
       int 0x80

換行改變行

 newline:          ; this changes the line
       push ecx
       mov eax, 4
       mov ebx, 1
       mov ecx, cr
       mov edx, 1
       int 0x80
       pop ecx
       ret

當我用常量替換 num 時,代碼運行良好

mov ecx,5

我認為這與 num 的數據類型有關。

在NASM等的指示mov ecx, num在寄存器NUM的負荷的地址。 要獲取num指向的地址中存儲的內容,您需要使用方括號,如mov ecx, [num]

您得到的輸入是文本,但您的循環需要一個實際的數字作為其循環計數器。 您需要進行轉換。

假設您的輸入由一位數組成,這是一個很好的解決方案:

       movzx ecx, byte [num]  ; Fetch the single digit input from the buffer
       sub   cl, '0'          ; Conversion "0".."9" -> 0..9
       jz    Done             ; No lines requested
li: 
       call  newline
       push  ecx
       mov   eax, 4
       mov   ebx, 1 
       mov   ecx, hello
       mov   edx, lenhello
       int   80h
       pop   ecx
       dec   ecx
       jnz   li
Done:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM