将两个字符串写入控制台不起作用。

huangapple go评论50阅读模式
英文:

Writing two strings to console not working

问题

这是您期望的输出:

0000
1111

您之前的代码有一些问题。在第二个字符串输出之后,您没有添加换行符 (0xA),这导致输出没有分行。此外,您的长度变量 lenotherLen 应该是 dd 类型而不是 db 类型,以确保它们被正确地加载为 32 位整数。以下是已经更正的代码:

section .data
    str: db "0000", 0xA
    len: dd 5
    otherStr: db "1111", 0xA
    otherLen: dd 5

global _start

section .text
_start:
    ; system call to print the first string
    mov eax, 4   ; "write" system call
    mov ebx, 1   ; Indicates to write to standard out
    mov ecx, str ; The string
    mov edx, [len] ; The length of the string
    int 0x80

    ; system call to print the second string
    mov eax, 4   ; "write" system call
    mov ebx, 1   ; Indicates to write to standard out
    mov ecx, otherStr ; The string
    mov edx, [otherLen] ; The length of the string
    int 0x80

    ; exit the program
    mov eax, 1
    mov ebx, 0
    int 0x80

请注意,我更正了长度变量的类型,以确保它们以正确的方式加载。希望这次的代码会输出您期望的结果。使用与您之前相同的汇编和链接命令进行编译和运行。

英文:

I’m new to x_86 assembly language, and trying write some simple stuff to the console.

When I try this:

	section .data
		str: db "0000", 0xA
		len: db 5

	global _start

	section .text
	_start:
		; system call to print the string
		mov eax, 4	; "write" system call
		mov ebx, 1	; Indicates to write to standard out
		mov ecx, str    ; The string
		mov edx, [len]  ; The length of the string
		int 0x80

		; exit the program
		mov eax, 1
		mov ebx, 0
		int 0x80

It works fine, and prints out “0000”. But if I then try this:

	section .data
		str: db "0000", 0xA
		len: db 5
		otherStr: db "1111", 0xA
		otherLen: db 5

	global _start

	section .text
	_start:
		; system call to print the string
		mov eax, 4	; "write" system call
		mov ebx, 1	; Indicates to write to standard out
		mov ecx, str    ; The string
		mov edx, [len]  ; The length of the string
		int 0x80

		; system call to print the string
		mov eax, 4	; "write" system call
		mov ebx, 1	; Indicates to write to standard out
		mov ecx, otherStr	; The string
		mov edx, [otherLen]     ; The length of the string
		int 0x80

		; exit the program
		mov eax, 1
		mov ebx, 0
		int 0x80

It outputs all this gobblygook:

	0000
	1111
		
	(#
	/
		6
		sha.asmstrlenotherStrotherLen__bss_start_edata_end.symtab.strtab.shstrtab.text.dat:! 
																									;!'1111

The output I’m expecting is just:

0000

1111

What am I doing wrong here? This is on Fedora Linux and using the NASM assembler. The commands I’m using to link and assemble are these:

nasm -f elf32 example.asm -o ex1.o
ld -m elf_i386 ex1.o -o ex1

答案1

得分: 3

len 的定义如下:

len: db 5

单独来看,这没问题。

它的加载方式如下:

mov edx, [len]

单独来看,这也没问题。

但是放在一起就有问题了。加载操作加载了一个DWORD,而变量只有一个字节的空间,因此加载了3个额外的字节,这些字节是"something else"。在第一个程序中,它们是用于填充数据段到页面大小的倍数的“零填充”的零。在第二个程序中,它们不是零。

你可以将长度(两者都是)改为DWORD(例如len: dd 5),或者使用零扩展来加载,例如movzx edx, byte [len]

英文:

len is defined like this

len: db 5

By itself that's fine.

It's loaded like this:

mov edx, [len]

By itself that's also fine.

But together they're wrong. The load loads a DWORD, and there's only one byte dedicated to the variable, so 3 extra bytes are loaded that are "something else". In the first program, they're zeroes from the "zero fill" to pad out the data section to a multiple of the page size. In the second program, they're not zero.

You could make the lengths (both of them) DWORD (eg len: dd 5) or load them with zero-extension eg movzx edx, byte [len].

huangapple
  • 本文由 发表于 2023年6月8日 23:41:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定