assembly 程序集将第二条消息打印两次

at0kjp5o  于 2023-11-19  发布在  其他
关注(0)|答案(1)|浏览(117)

我正在学习nasm assembly for Linux的子程序(运行在kali机器上)。我写了下面的代码,输出打印了两次“Hello World 2\n”,我不知道为什么。有人能解释一下吗?

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

字符串

ubof19bj

ubof19bj1#

感谢这些注解,我能够弄清楚要么cmp byte [eax], 0需要更改为cmp byte [eax], 0Ah,这不会保留换行符,要么我可以将,0添加到每条消息的末尾,这可以工作。
多谢帮忙!

相关问题