Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Linux中使用汇编将变量打印到命令行_Linux_Assembly_Nasm - Fatal编程技术网

在Linux中使用汇编将变量打印到命令行

在Linux中使用汇编将变量打印到命令行,linux,assembly,nasm,Linux,Assembly,Nasm,在尝试Linux汇编时,我遇到了以下问题。我才刚刚开始,所以我的程序是一个相对简单的程序,它来自于我在linuxassembly上找到的一些示例。它接受传递给命令行的第一个参数并将其打印出来。这是我到目前为止所拥有的 section .bss test_string: resb 3 section .text global _start _start: pop ebx ;argument number pop ebx ;program name

在尝试Linux汇编时,我遇到了以下问题。我才刚刚开始,所以我的程序是一个相对简单的程序,它来自于我在linuxassembly上找到的一些示例。它接受传递给命令行的第一个参数并将其打印出来。这是我到目前为止所拥有的

section .bss
    test_string: resb 3

section .text
    global _start

_start:
    pop ebx     ;argument number
    pop ebx     ;program name
    pop ebx     ;first argument
    mov [test_string],ebx

    mov eax,4
    mov ebx,1
    mov ecx,test_string
    mov edx,3
    int 80h

    mov eax,1
    mov ebx,0
    int 80h
我知道这篇文章写得很糟糕,但由于我是新手,我只是想在继续之前更好地理解汇编指令/变量是如何工作的。我组装和链接使用

nasm -f elf first.asm
ld -m elf_i386 -s -o first first.o
然后我用

./first one two

我原以为它会打印出
one
,但它会打印出乱七八糟的内容,比如
Y*&
。我做错了什么?我的
test\u字符串类型是否错误?

您试图打印指向字符串的指针值,而不是打印字符串。你想这样做

pop ebx     ;argument number
pop ebx     ;program name
pop ebx     ;pointer to the first argument

mov ecx,ebx ;load the pointer into ecx for the write system call

mov eax,4   ;load the other registers for the write system call
mov ebx,1
mov edx,3
int 80h

mov eax,1
mov ebx,0
int 80h

啊,我明白了。我正在加载指向测试字符串的指针。所以我应该做mov-ecx,[测试字符串]为什么
pop-ebx
只对
mov-ecx,ebx
?我认为您可以
弹出ecx
,并消除
mov ecx、ebx