Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Problem with runnig the first program.

programming

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1 SnakeS

SnakeS

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 24 September 2014 - 09:24 AM

Hi everyone.
my problem is about runnig a compolid program with terminal.

the assembly code :

section .text
global _start ;must be declared for linker (ld)
_start: ;tells linker entry point
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel

mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel

section .data
msg db 'Hello, world!', 0xa ;our dear string
len equ $ - msg ;length of our dear string

the terminal code :
 

SnakeS:~ SnakeS$ cd /Users/SnakeS/Desktop/
SnakeS:Desktop SnakeS$ nasm asmtest.asm
SnakeS:Desktop SnakeS$ ./asmtest
-bash: ./asmtest: Permission denied
SnakeS:Desktop SnakeS$ chmod 755 asmtest
SnakeS:Desktop SnakeS$ ./asmtest
-bash: ./asmtest: cannot execute binary file

I used this one too :
 

SnakeS:Desktop SnakeS$ nasm -f elf asmtest.asm
SnakeS:Desktop SnakeS$ chmod 755 asmtest.o
SnakeS:Desktop SnakeS$ ./asmtest.o
-bash: ./asmtest.o: cannot execute binary file
SnakeS:Desktop SnakeS$

How can I fix it?
thanks ;)


Edited by dargueta, 24 September 2014 - 09:35 AM.


#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 24 September 2014 - 09:39 AM

SnakeS:Desktop SnakeS$ nasm -o something -f elf asmtest.asm

SnakeS:Desktop SnakeS$ ./something

 

As I said before in your other thread, you're probably running the original object file from previous failed attempts.


sudo rm -rf / && echo $'Sanitize your inputs!'


#3 SnakeS

SnakeS

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 24 September 2014 - 12:14 PM

no I deleted everything except the source code and I compiled it again but It was like before :(

#4 Gunner

Gunner

    CC Regular

  • Member
  • PipPipPip
  • 47 posts

Posted 24 September 2014 - 06:53 PM

 

my problem is about running a compiled program with terminal

Your first problem is with your terminology!  When using Assembly, we do not compile anything, we use an Assembler to Assemble our source code into an object file THEN link the object files into an executable.

 

Look at your comments for these 2 lines:





global _start ;must be declared for linker (ld)
_start: ;tells linker entry point

Your talking about the linker, but do not use one.

 

 

 

Attached Thumbnails

  • 9-24-2014 10-51-37 PM.png


#5 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 24 September 2014 - 08:50 PM

I don't know how I missed that.  :worry:

 

Also, it's easier to just do:

nasm -f elf snakes.asm
gcc -o snakes snakes.o
./snakes

Just make sure you use main instead of _start as your entry point.

 

Note: If GCC barfs up an error, you'll have to use -f elf32 or -f elf64 instead of -f elf. If your computer has a 64-bit OS (most likely) then use -f elf64. Otherwise, use -f elf32.


Edited by dargueta, 24 September 2014 - 08:50 PM.

sudo rm -rf / && echo $'Sanitize your inputs!'


#6 SnakeS

SnakeS

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 25 September 2014 - 01:45 PM

unfortunately I couldn't call the linker :(

it is not my source code ,I copied it from http://www.tutorialspoint.com/ and I tried to run it but I can't :(

I don't understand anything from the source code

help me please

thanks ;)Screen Shot 2014-09-26 at 1.10.42 AM.png



#7 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 25 September 2014 - 01:50 PM

This is exactly what I was saying about compiling using -f elf64 instead of -f elf. Seriously, just use gcc instead of the /lib/ld-linux.so.2 stuff. It's easier to remember.


Edited by dargueta, 25 September 2014 - 01:51 PM.

sudo rm -rf / && echo $'Sanitize your inputs!'


#8 Gunner

Gunner

    CC Regular

  • Member
  • PipPipPip
  • 47 posts

Posted 25 September 2014 - 07:00 PM

Ok, it looks like you are using a 64 bit OS; which is fine.  Add the bold item to your ld command line:

 

ld -o snakes snakes.o -m elf_i386 -dynamic-linker /lib/lld-linux.so.2

 

It should now work.  If not, you will need to install the multilib, which I think is gcc-multilib.  

sudo apt-get gcc-multilib

or whatever package manager your system uses.  But why learn 32 bit Assembly?  Since you are using a 64bit OS, learn 64bit Assembly!

 

Also, please don't PM me to help you learn Assembly.  Keep all questions on the forum for all to benefit from.  You have the Internet today, there is tons of info regarding Assembly.  Try learning back in the day before the internet!



#9 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 25 September 2014 - 11:53 PM

But why learn 32 bit Assembly?  Since you are using a 64bit OS, learn 64bit Assembly!

 

Calling conventions in x86-32 are easier. Everything is pushed on the stack, whereas in x86-64 there are six registers used and you have to memorize their order. I'd recommend starting off with 32-bit to get a feel for how everything works.


Edited by dargueta, 25 September 2014 - 11:53 PM.

sudo rm -rf / && echo $'Sanitize your inputs!'


#10 SnakeS

SnakeS

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 26 September 2014 - 12:57 AM

I can't understand anything

I don't know anything about Assembly

and you told me "use 64bit Assembly" ?

Screen Shot 2014-09-26 at 12.26.08 PM.png

 

---------------------------------------------------

 

Screen Shot 2014-09-26 at 12.27.29 PM.png

 

 

I'm using Mac.

Please help me :((


Edited by SnakeS, 26 September 2014 - 12:58 AM.


#11 SnakeS

SnakeS

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 26 September 2014 - 05:11 AM

again didn't run.

please help :(

Screen Shot 2014-09-26 at 4.40.04 PM.png



#12 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 26 September 2014 - 07:28 AM

1) The full command is gcc -o asmtest asmtest.o

2) Stop trying to use ld, you don't know enough about it so it's confusing you. Like I said, use gcc.

 

You should probably learn assembly before you try running a program. Start here.


sudo rm -rf / && echo $'Sanitize your inputs!'





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download