汇编运行子程序两次

问题描述 投票:0回答:1

我正在学习Linux nasm汇编中的子例程(在kali机器上运行)。我编写了下面的代码,输出打印“Hello World 2 “两次,我不明白为什么。有人能解释一下吗?

section .data
msg db "Hello World", 0Ah     ; message 1
msg2 db "Hello World 2", 0Ah  ; message 2

section .text
global _start
_start:
    mov eax, msg          ; load message to registar
    call strlen           ; get length of string through subroutine
    mov edx, eax          ; sets string length
    mov eax, 4            ; kernel code for write
    mov ebx, 1            ; use std
    mov ecx, msg          ; buffer is msg
    int 80h                

    mov eax, msg2         ; does the same thing with msg2
    call strlen
    mov edx, eax
    mov eax, 4
    mov ebx, 1
    mov ecx, msg2
    int 80h               ; this gets run twice for some reason?

    mov eax, 1            ; exit with code 0
    mov ebx, 0
    int 80h

strlen:
    push ebx          ; use stack to preserve registar ebx
    mov ebx, eax      ; moves eax into the preserved registar

nextchar:
    cmp byte [eax], 0 ; compares the the byte to 0, if it is true,
    jz finished       ; jumps down or else increments one and
    inc eax           ; starts this part again
    jmp nextchar

finished:
    sub eax, ebx      ; subtracts the two values so that you get the
    pop ebx           ; string length, then restores ebx, and returns
    ret               ; to where it was called

linux assembly nasm subroutine
1个回答
0
投票

感谢这些评论,我能够弄清楚需要将

cmp byte [eax], 0
更改为
cmp byte [eax], 0Ah
,这不会保留换行符,或者我可以将
,0
添加到每条消息的末尾,这有效到.

感谢您的帮助!

© www.soinside.com 2019 - 2024. All rights reserved.