加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

使用linux程序集打印变量到命令行

发布时间:2020-12-14 02:40:24 所属栏目:Linux 来源:网络整理
导读:试试我的 Linux程序集,我遇到了以下问题.我刚刚开始,所以我的程序是一个相对简单的程序,源自我在linuxassembly上找到的一些例子.它将传递给命令行的第一个参数打印出来.这是我到目前为止所拥有的…… section .bss test_string: resb 3section .text global
试试我的 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 *&”这样的gibbrish.我究竟做错了什么?我的test_string是错误的类型吗?

解决方法

您正在尝试打印指向字符串的指针的值而不是打印字符串.你想要这样做.
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读