汇编循环的预期输出错误

时间:2013-08-13 21:21:43

标签: assembly x86 nasm

我没有得到预期的输出。我有一个循环应该继续10x,然后第二个循环应该继续10倍。循环应单独打印

section .data
    msg1:   db  "first",10,0
    msg2:   db  "second",10,0
    len1:   equ $-msg1
    len2:   equ $-msg2
section .bss
    num resb    1    ;reserve 1 byte
section .text
    global main
main:
    mov [num], BYTE 10d ;num = 10
    loop:
    mov edx,    len1
    mov ecx,    msg1
    mov ebx,    1
    mov eax,    4
    int 80h
    dec BYTE [num]      ; num--
    cmp [num], BYTE 0
    jnz loop        ; jump if not equal to zero

    mov [num], BYTE 20d ; num = 20
    loop2:
    mov edx,    len2
    mov ecx,    msg2
    mov ebx,    1
    mov eax,    4
    int 80h
    sub [num], BYTE 2   ; num = num - 2
    cmp [num], BYTE 0
    ja loop2        ; jump if above 0

    mov eax,    1
    mov ebx,    0
    int 80h

我到了 first second first second first second first second first second first second first second first second first second first second second second second second second second second second second second

但我期待first first first first first first first first first first second second second second second second second second second second

我是大会(NASM)的新手,我做错了什么?

1 个答案:

答案 0 :(得分:3)

问题在于你的定义:

section .data
msg1:   db  "first",10,0
msg2:   db  "second",10,0
len1:   equ $-msg1
len2:   equ $-msg2

这里你说msg1包括所有第一条消息和第二条消息。

那应该是

msg1:   db  "first",10,0
len1:   equ $-msg1

msg2:   db  "second",10,0
len2:   equ $-msg2