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

前端之家收集整理的这篇文章主要介绍了使用linux程序集打印变量到命令行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
试试我的 Linux程序集,我遇到了以下问题.我刚刚开始,所以我的程序是一个相对简单的程序,源自我在linuxassembly上找到的一些例子.它将传递给命令行的第一个参数打印出来.这是我到目前为止所拥有的……
  1. section .bss
  2. test_string: resb 3
  3.  
  4. section .text
  5. global _start
  6.  
  7. _start:
  8. pop ebx ;argument number
  9. pop ebx ;program name
  10. pop ebx ;first argument
  11. mov [test_string],ebx
  12.  
  13. mov eax,4
  14. mov ebx,1
  15. mov ecx,test_string
  16. mov edx,3
  17. int 80h
  18.  
  19. mov eax,1
  20. mov ebx,0
  21. int 80h

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

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

然后我继续使用..

  1. ./first one two

我当时认为它会打印出“one”,但它打印出像“Y *&”这样的gibbrish.我究竟做错了什么?我的test_string是错误的类型吗?

解决方法

您正在尝试打印指向字符串的指针的值而不是打印字符串.你想要这样做.
  1. pop ebx ;argument number
  2. pop ebx ;program name
  3. pop ebx ;pointer to the first argument
  4.  
  5. mov ecx,ebx ;load the pointer into ecx for the write system call
  6.  
  7. mov eax,4 ;load the other registers for the write system call
  8. mov ebx,1
  9. mov edx,3
  10. int 80h
  11.  
  12. mov eax,1
  13. mov ebx,0
  14. int 80h

猜你在找的Linux相关文章