汇编 x86,在字符串中的每个单词后打印新行
Assembly x86, print new line after each word in a string
需要在不同行的字符串中打印单独的标记,但无法打印出新行。
根据我的教程,它在示例中显示 "nwln",但它在我的代码中不起作用。
我现在的代码只在一行中打印所有单词,没有空格。
lab2:
cmp [ecx],byte ' '
je lab1
cmp [ecx], byte 0
je lab3
inc edx
inc ecx
jmp lab2
lab1:
sub ecx,edx
mov eax, SYS_WRITE
mov ebx, STDOUT
int 80h
add ecx, edx
inc ecx
mov edx,0
jmp lab2
好的,我想我已经通过在 .data 中使用另一个变量换行符和另一个寄存器来打印新行来解决它
因为您似乎在 Linux 上使用 NASM。 nwln
是一个宏,它是 NASM/Linux 的 Assembler Tutorial based on the code from the book Guide to Assembly Language Programming in Linux. The library is made up of a macro called nwln
that prints the LineFeed(LF) character 0x0a
to standard output. You can download the files io.mac
and io.o
from the link above. They are contained inside the ZIP file 的一部分。您将 io.mac
和 io.o
复制到您的项目目录。您必须在程序顶部包含宏文件。您的代码看起来像:
%include "io.mac"
SYS_EXIT equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
section .text
global main
main:
mov ecx,msg3
mov edx,0 ; Set the length
jmp lab2
lab2:
cmp [ecx],byte ' '
je lab1
cmp [ecx], byte 0
je lab3
inc edx
inc ecx
jmp lab2
lab1:
sub ecx,edx
mov eax, SYS_WRITE
mov ebx, STDOUT
int 80h
nwln
add ecx, edx
inc ecx
mov edx,0
jmp lab2
lab3:
sub ecx,edx
mov eax, SYS_WRITE
mov ebx, STDOUT
int 80h
nwln
mov eax, SYS_EXIT
int 80h
segment .data
msg3 db 'this string', 0x0
要在 32 位环境中编译和 link,您可以使用类似的东西:
nasm -f elf32 project.asm
ld -emain -melf_i386 -o project project.o io.o
您需要添加 io.o
作为 linker 对象来解析宏所需的函数。
如果您不想依赖 io.o
,下面的代码包含将以类似方式实现 nwln
的等效宏和函数:
%macro nwln 0
call proc_nwln
%endmacro
SYS_EXIT equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
section .text
global main
main:
mov ecx,msg3
mov edx,0 ; Set the length
jmp lab2
lab2:
cmp [ecx],byte ' '
je lab1
cmp [ecx], byte 0
je lab3
inc edx
inc ecx
jmp lab2
lab1:
sub ecx,edx
mov eax, SYS_WRITE
mov ebx, STDOUT
int 80h
nwln
add ecx, edx
inc ecx
mov edx,0
jmp lab2
lab3:
sub ecx,edx
mov eax, SYS_WRITE
mov ebx, STDOUT
int 80h
nwln
mov eax, SYS_EXIT
int 80h
proc_nwln:
pusha
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, new_line
mov edx, 0x1
int 0x80
popa
ret
segment .data
msg3 db 'this string', 0x0
new_line db 0x0a
然后你可以用类似的东西编译:
nasm -f elf32 project.asm
ld -emain -melf_i386 -o project project.o
需要在不同行的字符串中打印单独的标记,但无法打印出新行。
根据我的教程,它在示例中显示 "nwln",但它在我的代码中不起作用。
我现在的代码只在一行中打印所有单词,没有空格。
lab2:
cmp [ecx],byte ' '
je lab1
cmp [ecx], byte 0
je lab3
inc edx
inc ecx
jmp lab2
lab1:
sub ecx,edx
mov eax, SYS_WRITE
mov ebx, STDOUT
int 80h
add ecx, edx
inc ecx
mov edx,0
jmp lab2
好的,我想我已经通过在 .data 中使用另一个变量换行符和另一个寄存器来打印新行来解决它
因为您似乎在 Linux 上使用 NASM。 nwln
是一个宏,它是 NASM/Linux 的 Assembler Tutorial based on the code from the book Guide to Assembly Language Programming in Linux. The library is made up of a macro called nwln
that prints the LineFeed(LF) character 0x0a
to standard output. You can download the files io.mac
and io.o
from the link above. They are contained inside the ZIP file 的一部分。您将 io.mac
和 io.o
复制到您的项目目录。您必须在程序顶部包含宏文件。您的代码看起来像:
%include "io.mac"
SYS_EXIT equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
section .text
global main
main:
mov ecx,msg3
mov edx,0 ; Set the length
jmp lab2
lab2:
cmp [ecx],byte ' '
je lab1
cmp [ecx], byte 0
je lab3
inc edx
inc ecx
jmp lab2
lab1:
sub ecx,edx
mov eax, SYS_WRITE
mov ebx, STDOUT
int 80h
nwln
add ecx, edx
inc ecx
mov edx,0
jmp lab2
lab3:
sub ecx,edx
mov eax, SYS_WRITE
mov ebx, STDOUT
int 80h
nwln
mov eax, SYS_EXIT
int 80h
segment .data
msg3 db 'this string', 0x0
要在 32 位环境中编译和 link,您可以使用类似的东西:
nasm -f elf32 project.asm
ld -emain -melf_i386 -o project project.o io.o
您需要添加 io.o
作为 linker 对象来解析宏所需的函数。
如果您不想依赖 io.o
,下面的代码包含将以类似方式实现 nwln
的等效宏和函数:
%macro nwln 0
call proc_nwln
%endmacro
SYS_EXIT equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
section .text
global main
main:
mov ecx,msg3
mov edx,0 ; Set the length
jmp lab2
lab2:
cmp [ecx],byte ' '
je lab1
cmp [ecx], byte 0
je lab3
inc edx
inc ecx
jmp lab2
lab1:
sub ecx,edx
mov eax, SYS_WRITE
mov ebx, STDOUT
int 80h
nwln
add ecx, edx
inc ecx
mov edx,0
jmp lab2
lab3:
sub ecx,edx
mov eax, SYS_WRITE
mov ebx, STDOUT
int 80h
nwln
mov eax, SYS_EXIT
int 80h
proc_nwln:
pusha
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, new_line
mov edx, 0x1
int 0x80
popa
ret
segment .data
msg3 db 'this string', 0x0
new_line db 0x0a
然后你可以用类似的东西编译:
nasm -f elf32 project.asm
ld -emain -melf_i386 -o project project.o