字符串输出中的垃圾处理 [英] Garbage in string output function

查看:102
本文介绍了字符串输出中的垃圾处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在asm中编写一个printf替换,到目前为止已经有以下代码:

I'm trying to write a printf replacement in asm and so far have this code:

; string is loaded into r8
print_string:
    push rax
    push rbx
    push rsi
    push rdx

    ; load string pointer into ecx
    mov rsi, r8

    ; loop over every char
    print_string_loop0:
        cmp sil, 0 ; stop when encounter null character
        je print_string_return
        mov rax, 1 ; syscall (sys_write)
        mov rdi, 1 ; file descriptor for write (stdout = 1)
        mov rdx, 1 ; bytes to write (1 character)
        syscall
        inc rsi
        jmp print_string_loop0:

    print_string_return:
        pop rdx
        pop rsi
        pop rbx
        pop rax

可以,但是我总是在打印字符串后得到一些垃圾.

which works, but I always get some sort of garbage after the string that I print.

此处使用了print_string

global _start

section .text

_start:
    mov r8, string
    call print_string

    mov rax, 60 ; syscall (sys_exit)
    mov rdi, 0  ; exit code
    syscall

.section data

string:
    db "Hell! Oh, World.", 10, 0 ; string, newline, null

print_string是在同一文件中定义的.

print_string is defined in the same file.

那为什么在字符串后面打印垃圾?每次垃圾都是一样的,如果我完全修改程序集,则会输出不同的垃圾.

So why is garbage being printed after my string? The garbage is the same every time and if I modify the assembly at all, different garbage is output.

推荐答案

您在R8中输入了地址,因此在RSI中输入了地址而不是字符.因此,将中断条件cmp sil, 0更改为cmp byte [rsi], 0.

You've got in R8 and thus in RSI an address not a character. So change the break condition cmp sil, 0 to cmp byte [rsi], 0.

这篇关于字符串输出中的垃圾处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆