Linux汇编--打印unicode时不需要printf。

问题描述 投票:0回答:1

有没有一种方法可以在不使用printf的情况下将unicode字符打印到Linux控制台?

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

linux assembly unicode x86 system-calls
1个回答
2
投票

如果用 纯x86汇编 你的意思是不需要includelink任何第三方库,那么是的,你可以用内核服务打印到控制台。撰写. 我觉得不比使用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.

在64位模式下

nasm -f ELF64 sample.asm -o sample.o
ld sample.o -o sample 
./sample
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.