Linux Assembly - 打印 unicode 而不打印 printf



有没有办法在不使用printf的情况下将Unicode字符打印到Linux控制台?

我知道printf是"正确"的方法,我只是想知道使用纯x86程序集是否可行。

如果使用纯 x86 程序集,您的意思是不必包含/链接任何第三方库,那么是的,您可以使用内核服务写入打印到控制台。我不认为它比使用 C 库更正确。我将以下用 UTF-8 编码的示例保存为"sample.asm",然后组装、链接并运行

nasm -f ELF32 sample.asm -o sample.o
ld sample.o -o sample -m elf_i386
./sample

它按预期工作。

SEGMENT .data
Sample DB "Sample text of mixed alphabets:",10
DB "Éireannach (Eireannach in western European alphabet)",10
DB "Čapek (Capek in central European alphabet)",10
DB "Ørsted (Oersted in Nordic alphabet)",10
DB "Aukštaičių (Aukshtaiciu in Baltic alphabet)",10
DB "Ὅμηρος (Homer in Greek alphabet)",10
DB "Yumuşak ğ (Yumushak g in Turkish aplhabet)",10
DB "Maðkur (Mathkur in Icelandic alphabet)",10
DB "דגבא (ABGD in Hebrew alphabet)",10
DB "Достоевский (Dostoevsky in Cyrillic alphabet)",10
DB 0
SizeOfSample EQU $ - Sample
SEGMENT .text
GLOBAL _start:
_start:MOV EAX,4      ; Kernel function sys_write in 32bit mode.
MOV EBX,1      ; File descriptor of standard output.
MOV ECX,Sample ; Offset of the written text.
MOV EDX,SizeOfSample ; Number of bytes (not characters).
INT 0x80       ; Invoke kernel service.
MOV EAX,1      ; Kernel function sys_exit in 32bit mode.
INT 0x80       ; Invoke kernel service.

下面是 64 位模式下的相同示例:

SEGMENT .data
Sample DB "Sample text of mixed alphabets:",10
DB "Éireannach (Eireannach in western European alphabet)",10
DB "Čapek (Capek in central European alphabet)",10
DB "Ørsted (Oersted in Nordic alphabet)",10
DB "Aukštaičių (Aukshtaiciu in Baltic alphabet)",10
DB "Ὅμηρος (Homer in Greek alphabet)",10
DB "Yumuşak ğ (Yumushak g in Turkish aplhabet)",10
DB "Maðkur (Mathkur in Icelandic alphabet)",10
DB "דגבא (ABGD in Hebrew alphabet)",10
DB "Достоевский (Dostoevsky in Cyrillic alphabet)",10
DB 0
SizeOfSample EQU $ - Sample
SEGMENT .text
GLOBAL _start:
_start:MOV RAX,1        ; Kernel function sys_write in 64bit mode.
MOV RDI,1        ; File descriptor of standard output.
LEA RSI,[Sample] ; Offset of the written text.
MOV RDX,SizeOfSample ; Number of bytes (not characters).
SYSCALL          ; Invoke kernel service.
MOV EAX,60       ; Kernel function sys_exit in 64bit mode.
SYSCALL          ; Invoke kernel service.

创建方式为

nasm -f ELF64 sample.asm -o sample.o
ld sample.o -o sample 
./sample

最新更新