Author Topic: Could use some Linux code to get me started  (Read 7887 times)

Offline fixit

  • Jr. Member
  • *
  • Posts: 23
Could use some Linux code to get me started
« on: May 10, 2014, 09:59:09 PM »
I can not find any Linux assembly code here ?

Everything I found was for Windows.

Thanks.

Offline Gunner

  • Jr. Member
  • *
  • Posts: 74
  • Country: us
    • Gunners Software
Re: Could use some Linux code to get me started
« Reply #1 on: May 11, 2014, 06:26:50 AM »
Something you said in the FASM forum:
Quote
I had to give up on Nasm because of a lack of help.

Seriously?  C'mon Andy.  Every Assembler I have seen is command line only.  Windows spoiled you!!!  IDEs and whatnot hide the command line from you.  Linux is a command line OS.  Why give up?  You already know MASM, there is not much of a difference between that and NASM or FASM.  You just need to learn a new OS.  Linux uses interrupts instead of an API.  32 bit uses int 80H and 64 bit uses syscall.  What is your CPU, 32 or 64 bit?  What are you looking to program?

64 bit syscall numbers are here https://sites.google.com/site/alpil0000/linux-64-bit-system-call-list
32 bit int 80H numbers are here https://sites.google.com/site/alpil0000/linux-32-bit-system-call-list
32 bit sycalls with what registers hold what params  http://syscalls.kernelgrok.com/
the man pages for the calls are here http://linuxmanpages.com/man2/

Here is a small tool I wrote using GTK+ and LibCuRL github.com/GunnerInc/ip-country-info
« Last Edit: May 11, 2014, 06:31:17 AM by Gunner »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2596
  • Country: us
Re: Could use some Linux code to get me started
« Reply #2 on: May 11, 2014, 07:08:03 AM »
Code: [Select]
; nasm -f elf32 myprog.asm
; ld -o myprog myprog.o -m elf_i386

global _start ; ld needs to know about this

section .text
; can put subroutines before your entrypoint, if you care to
_start:
; other code here
exit:
    mov eax, 1 ; sys_exit
    mov ebx, 42
    int 80h

Whatever else you do, you're going to have to exit cleanly, so you can't start much simpler than that! That won't produce any visible output, but if you type "echo $?" it will show the exit code of the last process to run. We normally use zero to indicate "normal termination" but I threw in 42 just to show that we did something. We say that the exit code is "in ebx" but only bl is actually useful. You can use this "trick" to display simple results like "2 + 2" or... I find it useful to display error numbers of system calls that fail...

Gunner (yes, the same Gunner who was so helpful on the AsmCommunity forum) has got some tutorials starting(?) here:

http://www.dreamincode.net/forums/topic/286248-nasm-linux-terminal-inputoutput-wint-80h/

There's some useful information if you poke around:
http://asm.sf.net
Some of it's for AT&T syntax, and covers BSD and other forms of Unix so it isn't all "Nasm for Linux" but much of it is.

We've got a lot of Linux code here - I think we're better for Linux than Windows, actually - but it's scattered around and not well labeled, so you'll have to poke around for it. I'll see if I can put together a few links if I get around to it, but as we've discussed I'm not very good at that lately...

Well, I see that Gunner is helpful as always... Thanks Gunner!

Best,
Frank


Offline fixit

  • Jr. Member
  • *
  • Posts: 23
Re: Could use some Linux code to get me started
« Reply #3 on: May 11, 2014, 08:19:16 PM »
I got frustrated when none of the executable examples ran, either from a konsole or file manager.

I have not given up.

I have my German mother's stubbornness. :-)

Andy

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2596
  • Country: us
Re: Could use some Linux code to get me started
« Reply #4 on: May 11, 2014, 09:23:23 PM »
Hi Andy,

Good! It's an appropriate day to appreciate what we got from our mothers, whether they're still around or not. Happy Mother's Day to anyone qualified! No reason you can't have a good day in any case.

There's a generic answer to "Why doesn't my code work?": "Something you believe to be true is not true." To get more specific than that, we'll probably need more information. What did you do? What happened? Nasm's job is to "shut up and assemble", ld's is to "shut up and link". If you see any output from either, pay close attention - often there's a clue there. When you try to run it, do you get no output, garbage output, "can't execute binary file", or what? The most confusing one I've seen is "no such file"... even though "myprog" is right there!  It's because, by default, ld looks for "/lib/ld-linux,so.1" and only "...so.2" exists. You shouldn't run into that unless you're trying to link against C (or other) libraries. Easiest fix is to use gcc, but we can tell ld the "right" interpreter/dynamic-linker, too.

Here's a 64-bit example. Maybe you'll have better luck with it.
Code: [Select]
; nasm -f elf64 myprog.asm
; ld -o myprog myprog.o

global _start

section .data
msg db "Hello 64-bit world!", 10
msg_size equ $ - msg

section .text
_start:

mov rdi, 1
mov rsi, msg
mov rdx, msg_size
mov rax, 1
syscall

mov rdi, 42
mov rax, 3Ch
syscall

As Betov used to say, "Courage!"

Best,
Frank


Offline fixit

  • Jr. Member
  • *
  • Posts: 23
Re: Could use some Linux code to get me started
« Reply #5 on: May 15, 2014, 05:01:53 AM »
Something you said in the FASM forum:
Quote
I had to give up on Nasm because of a lack of help.

Seriously?  C'mon Andy.  Every Assembler I have seen is command line only.  Windows spoiled you!!!  IDEs and whatnot hide the command line from you.  Linux is a command line OS.  Why give up?  You already know MASM, there is not much of a difference between that and NASM or FASM.  You just need to learn a new OS.  Linux uses interrupts instead of an API.  32 bit uses int 80H and 64 bit uses syscall.  What is your CPU, 32 or 64 bit?  What are you looking to program?


Gunner,

I still do boatloads of konsole work.

I have a 64 bit CPU, 2 core.

I would like to primarily  program GUI type programs.
[ I can live with Linux programs not having built in icons. :-) ]

As well as console ones 2.

Later,
           Andy

Offline nullptr

  • Jr. Member
  • *
  • Posts: 27
Re: Could use some Linux code to get me started
« Reply #6 on: May 15, 2014, 04:57:05 PM »
Hi Fixit,
here are links with example source code for 32 bits:
http://www.csee.umbc.edu/portal/help/nasm/sample.shtml
and for 64 bits:
http://cs.lmu.edu/~ray/notes/nasmexamples/

Offline fixit

  • Jr. Member
  • *
  • Posts: 23
Re: Could use some Linux code to get me started
« Reply #7 on: November 14, 2014, 06:21:51 AM »
Sorry for the late reply, but thanks for your help ?  :-)

Andy