在Linux中使用汇编将变量打印到命令行 [英] Printing variable to command line using assembly in Linux

查看:418
本文介绍了在Linux中使用汇编将变量打印到命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试Linux组装时,我遇到了以下问题.我刚开始,所以我的程序是一个相对简单的程序,源于我在linuxassembly中找到的一些示例.它采用传递给命令行的第一个参数并将其打印出来.这是我到目前为止所拥有的...

Trying my hand at Linux assembly and I'm running into the following problem. I'm just starting out so my program is a relatively simple one derived from some examples I found over at linuxassembly. It takes the first argument passed to the command line and prints it out. Here is what I have so far...

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

我知道这写得不好,但是由于我是新手,所以我只是想在继续学习之前更好地理解汇编指令/变量的工作方式.我使用...进行组装和链接.

I know that this is poorly written, but since I'm new to this, I'm just looking to better understand how assembly instructions/variables work before I move on. I assemble and link using...

nasm -f elf first.asm
ld -m elf_i386 -s -o first first.o

然后我使用..

./first one two

我本来以为它会打印出one,但它会打印出像Y*&一样乱码.我究竟做错了什么?我的test_string类型错误吗?

I was thinking that it would print out one but it prints out gibberish like Y*&. What am I doing wrong? Is my test_string the wrong type?

推荐答案

您正尝试打印出指向字符串的指针的值,而不是打印字符串.您要改为执行此操作.

You're trying to print out the value of the pointer to the string instead of printing the string. You want to do this instead.

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

这篇关于在Linux中使用汇编将变量打印到命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆