简体   繁体   中英

Assembly loop doesn't print values

I'm starting with assembly and am trying to create a simple loop to print eax values, but it doesn't work and I'm not totally sure what I'm doing here.

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

I couldn't understand your comments so i didn't really know what was going on, but i made an example program for you using a bss section to store your counter;

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

outputs;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM