簡體   English   中英

程序集循環不打印值

[英]Assembly loop doesn't print values

我從組裝開始,正在嘗試創建一個簡單的循環來打印eax值,但是它不起作用,而且我不確定我在這里做什么。

global  _main        ; make visible for linker
extern  _printf      ; link with printf
; -------------------------------
section .const
  hello db `Hello world! %d \n\0`; 0 on stringi lõpp.
  arv dw 5 ; %d võimaldab stringis arvu näidata.
  otsitav dw 10 ;10 on reavahetus
  vastus dw 0 ;dw läheb arvule
section .text
; -------------------------------
_main:
    mov eax, otsitav ; Annan eax-le kasutaja sisestatud väärtuse.
    mov ebx, 1 ; Annab ebx-le väärtuse 1 - sealt alustab for tsükliga.

    .loop1:
    dec eax ; võtab eax-ilt ühe ära.
    push eax
    call _printf
    add esp, 4 ; tasakaalustab.
    cmp eax, 0 ; eax ?  0
    je .loop1 ; kui ? asemele saab = panna siis hüppa .loop1 juurde
    ret

我聽不懂您的評論,所以我真的不知道發生了什么,但是我使用bss節為您制作了一個示例程序來存儲您的計數器;

global  _main
extern  _printf

[section] .bss
    storage resd 1      ; reserve 1 dword

[section] .data
  fmt   db "output = %s %d", 0dh,0ah,0 ; newline in formate now

  hello db "hello world",0  ; the string

  count equ 10              ; output ten times

[section] .text

_main:
    push ebp
    mov ebp, esp            ; stack related

    mov eax, count
    mov dword[storage], eax ; store eax's current value

nLoop:
    push eax        
    push hello
    push fmt
    call _printf
    add esp, 12             ; clean 3 args (4 * 3)

    mov eax, dword[storage] ; grab current count
    sub eax, 1
    mov dword[storage], eax ; store new value for later
    jnz nLoop               ; test if value is zero

    leave ; also stack related
    ret

輸出;

output = hello world 10
output = hello world 9
output = hello world 8
output = hello world 7
output = hello world 6
output = hello world 5
output = hello world 4
output = hello world 3
output = hello world 2
output = hello world 1

暫無
暫無

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

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