简体   繁体   中英

Printing variable to command line using assembly in Linux

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

Then I run using..

./first one two

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM