Jump to content

passing parameters to programs

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Robotnik

Robotnik

    Newbie

  • Members
  • Pip
  • 5 posts
I have been learning assembly with the gnu assembler and I am trying to pass a program a simple string parameter though terminal, and then create a file with that name. I have got this to work on my netbook with 32 bit ubuntu on it, but when I tried to make a similar prgm on my 64 bit computer it did not work. I know that the program is getting the address of the string from the stack, but it will not make a file out of it. I have also made a prgm that makes a file out of a string defined in the .data section, and it worked. This is the code I have. So the cmd './prgm file.txt' should create a file named file.txt, but it does not. If anyone knows what I am doing wrong please help.

.section .text

.globl _start

_start: 

  mov $5,%rax             # open file syscall number.

  mov 16(%rsp), %rbx      # move string pointer into %rbx from stack.

  mov $03101, %rcx        # set to write, or create if file does not exist.

  mov $0666, %rdx         # set permissions

 int $0x80                # call linux interrupt 


exit:

  mov $1, %rax 

 int $0x80  



#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Depending on how you linked your program, it might be mov 10(%rsp), %rbx
sudo rm -rf /

#3
Robotnik

Robotnik

    Newbie

  • Members
  • Pip
  • 5 posts
Thanks for the reply. I just tried that, I got a 'segmentation fault' error. I know that the program is getting the address of the string, but it just wont make a file out of it. It makes no sense to me.
Here is how I know the program is getting the string:
this program...
.section .text

.globl _start

_start:

  mov 16(%rsp), %rax  # move address of string from stack. (second parameter)

  mov (%rax), %rbx    # move first char of the string into %rbx. (return status)

  mov $1, %rax      

  int $0x80
returns the ascii char values of the inputed string from terminal. so I know i'm doing that correct. For instance, it returns 116 if the inputed string starts with a 't'.
And I made this program to test if I was making files correctly...

.section .data

string:

  .asciz "test.txt"


.section .text

.globl _start

_start:

  mov $5, %rax        # open file syscall number

  mov $string, %rbx   # address of first char in file name

  mov $03101, %rcx    # write to, or create file if it does not exist

  mov $0666, %rdx     # permission

  int $0x80


exit:

  mov $1, %rax

  int $0x80

and it works... I get a text file called test.txt
So I have no idea why the first program does not work...

This is the cmd Im using for linking my programs. "ld prgm.o -o prgm"




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users