用printf组件打印新行 [英] printing new lines with printf assembly

查看:160
本文介绍了用printf组件打印新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我正在尝试编写一些使用printf打印给定字符串的汇编代码。我在.data部分使用之前声明了我的字符串,测试示例如下所示: extern fflush

LINUX equ 80H;进入Linux内核的中断号
EXIT equ 60; Linux系统调用1 ie exit()

section .data
outputstringfmt:db%s,0
sentence0:dbHello\\\
World\\\
,0


segment .text
全球主要


主要:
mov r8,句子0
push r8
call print_sentence
add rsp,8
call os_return
$ b print_sentence:
push rbp
mov rbp,rsp
push r12
mov r12,[rbp + 16]
push rsi
push rdi
push r8
push r9
push r10
mov rsi,r12
mov rdi,outputstringfmt
xor rax,rax
call printf
xor rax,rax
call fflush
pop r10
pop r9
pop r8
pop rdi
pop rsi
pop r12
pop rbp
ret

os_return:
mov rax,EXIT ; Linux系统调用1即exit()
mov rdi,0;错误代码0即没有错误
系统调用;中断Linux内核64位

然后编译如下:

  nasm -f elf64 test.asm; gcc -m64 -o test test.o 

最后运行:

  ./ test 

我的输出如下所示:

  Hello \\\
World\\\

我真的不想将sentence0分成以下几种:

  sentence0:dbHello,10,0 
sentence1:dbWorld,10,0

,然后打印两次。有没有更好的方法来做到这一点?



预先感谢!

解决方案

NASM接受单引号('...')或双引号(...),它们是相同的,并且不提供任何逃脱;或反引号(` ...` ),它们提供对C风格转义的支持,这正是您想要的。



(请参阅第3.4.2节字符串, )



所以,试试:

  sentence0:db`Hello \\\
World\\\
`,0


Hi I'm trying to write some assembly code that uses printf to print a given string. I am declaring my strings before use in the .data section and a test example looks as follows:

extern printf
extern fflush

LINUX        equ     80H      ; interupt number for entering Linux kernel
EXIT         equ     60       ; Linux system call 1 i.e. exit ()

section .data
    outputstringfmt: db "%s", 0
    sentence0: db "Hello\nWorld\n", 0


segment .text
    global  main


main:
    mov r8, sentence0
    push r8
    call print_sentence
    add rsp, 8
    call os_return

print_sentence:
    push rbp
    mov rbp, rsp
    push r12
    mov r12, [rbp + 16]
    push rsi
    push rdi
    push r8
    push r9
    push r10
    mov rsi, r12
    mov rdi, outputstringfmt
    xor rax, rax
    call printf
    xor rax, rax
    call fflush
    pop r10
    pop r9
    pop r8
    pop rdi
    pop rsi
    pop r12
    pop rbp
    ret

os_return:
    mov  rax, EXIT      ; Linux system call 1 i.e. exit ()
    mov  rdi, 0     ; Error code 0 i.e. no errors
    syscall     ; Interrupt Linux kernel 64-bit

I'm then compiling as follows:

nasm -f elf64 test.asm; gcc -m64 -o test test.o

And finally running:

./test

My output is as follows:

Hello\nWorld\n

I really don't want to split sentence0 up into the following:

sentence0: db "Hello", 10, 0
sentence1: db "World", 10, 0

and then call the print twice. Is there a better way to do it?

Thanks in advance!

解决方案

NASM accepts strings in single quotes ('...') or double quotes ("..."), which are equivalent, and do not provide any escapes; or in backquotes (`...`), which provide support for C-style escapes, which is what you want.

(See section 3.4.2, "Character Strings", in the documentation.)

So, try:

sentence0: db `Hello\nWorld\n`, 0

这篇关于用printf组件打印新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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