Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set color when calling printf in assembly

I want to set the color when I call printf from assembly.

This is my code:

Out:    
    mov     rdi, answer
    mov     rsi, r10
    mov     rax,0
    call    printf
section .data
    answer:     db       "\033[0;31m%d\033[0m",10,0

I use NASM to compile and gcc to link:

nasm -f elf64 "%f"
gcc -o %e %e.o

However, the output is:

\033[0;31m(my r10)\033[0m
like image 708
CXWorks Avatar asked Dec 13 '25 02:12

CXWorks


1 Answers

Use ` for surrounding strings to have escape sequence work in NASM.

Reference: 3.4.2 Character Strings

Try this:

Out:
    mov     rdi, answer
    mov     rsi, r10
    mov     rax,0
    call    printf
section .data
    answer:     db       `\033[0;31m%d\033[0m`,10,0
like image 120
MikeCAT Avatar answered Dec 15 '25 23:12

MikeCAT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!