Search code examples
linuxassemblyconsoleescapingnasm

Why does not console accept reverse line feed?


So, I want to create a game of life simulator on the console. For that, I need to move the cursor up so I can rewrite on it and I can do that with \u008d.

I try that on the console:

echo -e "Everyone\n\u008dHi"

This gives me the result:

Hieryone

This is the expected result, and proves that my console accepts that character. But, when I try the exact same thing in NASM,

  global _start

  segment .text

_start:
  ;; Print message
  mov   rax, 1
  mov   rdi, 1
  mov   rsi, message
  mov   rdx, length
  syscall

  ;; Exit
  mov   rax, 60
  mov   rdi, 0
  syscall

  segment .data
message: db "Everyone", 0xa, 0x8d, "Hi"
length:  equ $ - message

And run it like,

nasm example.asm -f elf64 -o output.o
ld output.o -o output
./output

I get,

Everyone
�Hi

Why is that? Is there something that changes the way this character is interpreted in my NASM program? Am I missing something in the Unicode? Or is there another alternative that would allow me to move the cursor back? Any help would be useful. (I am on Linux btw)


Solution

  • I suggest you compare it with the hexdump.

    $ echo -e "Everyone\n\u008dHi" | hexdump -C
    00000000  45 76 65 72 79 6f 6e 65  0a c2 8d 48 69 0a        |Everyone...Hi.|
    0000000e
    
    $ ./output | hexdump -C
    00000000  45 76 65 72 79 6f 6e 65  0a 8d 48 69              |Everyone..Hi|
    0000000c
    

    As you see, the difference is whether you have 0xc2 or not (except for newline 0x0a). In UTF-8, 0x8d must be represented as 0xc2 0x8d.

    Therefore, the following modifications will give the expected results.

    message: db "Everyone", 0xa, 0xc2, 0x8d, "Hi"