装配循环不打印值

时间:2014-01-27 19:11:16

标签: loops assembly printing x86

我从汇编开始,我正在尝试创建一个简单的循环来打印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

1 个答案:

答案 0 :(得分:0)

我无法理解你的评论,所以我真的不知道发生了什么,但我为你制作了一个示例程序,使用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