Printing variable to command line using assembly i

2020-02-15 00:55发布

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?

1条回答
叼着烟拽天下
2楼-- · 2020-02-15 01:15

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
查看更多
登录 后发表回答