2

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 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, I see. I was loading the pointer to test_string. So instead I should have done mov ecx, [test_string]
Why pop ebx only to mov ecx, ebx? I think you can pop ecx, and eliminate the mov ecx,ebx.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.