Author Topic: Learning Assembly Language Programming using nasm in Fedora 17  (Read 15916 times)

Offline S Mahen

  • Jr. Member
  • *
  • Posts: 21
I downloaded and installed nasm-2.10.07 on FEDORA-17

Tried with "Hello World" code and it is working fine.

Earlier I wrote 16bit ALP in windows environment in which I was using INT 21h, INT 10h etc.

As a beginner, I am finding it difficult to get resources about understanding different things about ALP in NASM.  :'(

I am looking for step by step resource to start ALPing in nasm on fedora 17. The information about "Assembler Directives", "Syntax", "INT 80h functions", "Sample Codes(to understand)" ... every thing that is required to become comfortable with ALP in nasm on fedora 17.

It will be really helpful for forum's support to start with ALPing in nasm.

Thank you in advance.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2616
  • Country: us
Re: Learning Assembly Language Programming using nasm in Fedora 17
« Reply #1 on: July 14, 2013, 06:32:39 AM »
Good. If you've got "hello world" working, you're on your way!

You can learn Nasm's directives, syntax, command line switches, etc. from the Manual.

For int 80h routines and info, your best bet might be to download and install this rather large file from Jeff Owens:
http://home.myfairpoint.net/fbkotler//asmtools-0.9.69.tar.gz

To be honest, I don't use much of the stuff in this package, but I can't recommend "asmref" highly enough. It's got all the system calls listed by name and by number, with examples of how to use 'em from assembly. There's much, much more - instruction set reference(s)... well, look at it. I think you'll find it useful.

There are other tutorials, etc. located at http://asm.sourceforge.net - the "asmutils" package includes a great set of "%include" files. Since it works with other related OSen besides Linux, an OS has to be defined which makes it kind of a PITA to use (IMO), but I think it has a larger set of structures and  constants defined than in Jeff's work above.

Paul Carter's tutorial - http://www.paulcarter.com/pcasm - will work with Nasm on Windows, Linux, and others. It does this magic by hiding all the OS-specific stuff behind your back, using the C library. Not much related directly to Linux, but it's a good introduction to 32-bit programming in general.

There's more, but that'll get you started...

Best,
Frank


Offline S Mahen

  • Jr. Member
  • *
  • Posts: 21
Re: Learning Assembly Language Programming using nasm in Fedora 17
« Reply #2 on: July 16, 2013, 03:13:08 PM »
Thanks Frank.

You have given me very right path for starting assembly programming using nasm.

I'm following steps mentioned by you. I got the reference of INT 80h functions from asmref.

I was trying following code to display "hello world" using syscall

##########
section .data

message:
    db      'hello, world!', 0

section .text

global _start
_start:
    mov     rax, 4
    mov     rdi, 1
    mov     rsi, message
    mov     rdx, 13
    syscall

    mov     rax, 1
    xor     rdi, rdi
    syscall

###########
[(none) Documents]$ nasm -f elf64 hw.asm
[(none) Documents]$ ld -o hw hw.o
[@(none) Documents]$ ./hw
hello, world!Segmentation fault (core dumped)
[@(none) Documents]$

###########
After execution it is giving message

Segmentation fault (core dumped)

Why?

What is the difference in using int 80h and syscall?
Again the same help "From where I will get complete information of using syscall with assembly language"

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2616
  • Country: us
Re: Learning Assembly Language Programming using nasm in Fedora 17
« Reply #3 on: July 16, 2013, 05:19:53 PM »
Ahhh... looks like you're using the 32-bit system call numbers in 64-bit code. I thought we had 64-bit system call numbers somewhere in the Examples section, but all I can find now are the 32-bit numbers. Here is one such list:
http://blog.rchapman.org/post/36801038863/linux-system-call-table-for-x86-64

64-bit tutorials are harder to find. I don't have any in mind. There are some examples in the Examples section here.

Here's my attempt to "fix" your code:
Code: [Select]
section .data

message:
    db      'hello, world!', 10 ; zero terminator not useful here
    message_size equ $ - message

section .text

global _start
_start:
    mov     rax, 1 ; sys_read
    mov     rdi, 1  ; stdout
    mov     rsi, message
    mov     rdx, message_size
    syscall

    mov     rax, 60 ; sys_exit
    xor     rdi, rdi
    syscall

This is untested! I'm not yet running a 64-bit system. Soon... soon...

Best,
Frank



Offline S Mahen

  • Jr. Member
  • *
  • Posts: 21
Re: Learning Assembly Language Programming using nasm in Fedora 17
« Reply #4 on: August 08, 2013, 04:07:13 PM »
I wrote different codes using your help.

Accepting number, Displaying numbers, performing arithmetic operations etc.

Now this is the step where I want to know the best debugger to be used in Fedora 17.

Please guide me Which debugger is user friendly and gives proper user interface?

I tried ddd but it is giving me error while installing.

I followed steps after extracting ddd-3.3.12.tar.gz

$./configure && make
.
.
checking for connect... yes
checking for gethostbyname... yes
checking for regcmp... no
checking for regcmp in -lgen... no
checking for yyerror in -ly... no
checking for sin in -lm... yes
checking for open in -lc... yes
checking for elf_version in -lelf... no
checking for tgetent in -lmytinfo... no
checking for tgetent in -lncurses... no
checking for tgetent in -lcurses... no
checking for tgetent in -ltermcap... no
checking for tgetent in -lterminfo... no
checking for tgetent in -ltermlib... no
configure: error: Cannot find termcap compatible library.
[mahendra@(none) ddd-3.3.12]$ make install
make: *** No rule to make target `install'.  Stop.
[mahendra@(none) ddd-3.3.12]$

Please help me or suggest other best debugger for debugging ALPs.

Offline dogman

  • Jr. Member
  • *
  • Posts: 51
Re: Learning Assembly Language Programming using nasm in Fedora 17
« Reply #5 on: August 08, 2013, 06:04:26 PM »

Now this is the step where I want to know the best debugger to be used in Fedora 17.

Please guide me Which debugger is user friendly and gives proper user interface?

About the only debugger for Linux is gdb. There are a few other choices but they cost money or involve installing a huge toolchain and probably are not for you if you just want a debugger to use with asm.

gdb is command line. There are a few GUI front ends to gdb, none of them very good. They will all break or make your code look broken when it's not, or change some critical piece of something you're testing or do something else you don't like sooner or later. I haven't tried kdbg but I read about it in Duntemann's book. I don't know if it's still maintained and I don't install kde libs so I never used it. I believe Fedora is gnome but if you have kde libs or don't mind a few hundred meg (few gigs?) of extra kde stuff maybe try kdgb. I don't know whether kdbg is supposed to stand for "kde debug" or whether it's kgbd as in "kde gdb". Check a few variations in case my dyslexia made things worse.

insight seems to be the best of the worst but the builds often break and the stable version is unusable for 64 bit Intel. Does it sound like there aren't any good choices? ddd has a very weird UI. It seems to have lots of capabilities but it was designed by an idiot or a sadist, it's hard to tell. Writing assembly on Linux is much harder and a lot less fun than it should be. Thank you, C-programmers. Did you say "proper UI?" Alas, proper UIs and Linux are not to be found together.

I'm starting to lean towards recommending people use NetBeans IDE for assembly programming on Linux. NetBeans is far from perfect but it's relatively heavily used compared to debugger front ends so the main features seem to work. The nice thing for assembly programmers is the GUI debugger front end. It is made to look like Solaris' dbxtool and it's very nice and featureful. Not perfect, but nothing on Linux is. It's a shame to have to use an IDE that needs a full gig of RAM just to start for a few hundred bytes of assembly code, but there you have it.

I tried ddd but it is giving me error while installing.

I followed steps after extracting ddd-3.3.12.tar.gz

$./configure && make
.
.
checking for connect... yes
checking for gethostbyname... yes
checking for regcmp... no
checking for regcmp in -lgen... no
checking for yyerror in -ly... no
checking for sin in -lm... yes
checking for open in -lc... yes
checking for elf_version in -lelf... no
checking for tgetent in -lmytinfo... no
checking for tgetent in -lncurses... no
checking for tgetent in -lcurses... no
checking for tgetent in -ltermcap... no
checking for tgetent in -lterminfo... no
checking for tgetent in -ltermlib... no
configure: error: Cannot find termcap compatible library.
[mahendra@(none) ddd-3.3.12]$ make install
make: *** No rule to make target `install'.  Stop.
[mahendra@(none) ddd-3.3.12]$

Please help me or suggest other best debugger for debugging ALPs.

looks like you're missing the curses development package. I don't use a package-managed Linux so I don't know which tools you have but use whatever search powers you have an see if ddd is already packaged. That will save you lots of problems. If you can't find a package and want to build it as above you're going to need to be a lot more resourceful in finding dependencies and resolving them. I don't think that's what Fedora is about. If all else fails try installing the (n)curses development package and try again. Keep resolving deps until you can't resolve 'em no mo.
« Last Edit: August 08, 2013, 06:09:51 PM by dogman »

Offline dogman

  • Jr. Member
  • *
  • Posts: 51
Re: Learning Assembly Language Programming using nasm in Fedora 17
« Reply #6 on: August 08, 2013, 06:06:28 PM »
accidental double post

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2616
  • Country: us
Re: Learning Assembly Language Programming using nasm in Fedora 17
« Reply #7 on: August 09, 2013, 10:15:03 AM »
Found it! This is the late Chuck Crayne's ".gdbinit":

------------------------------------------------------------------
    * From: Chuck Crayne <ccrayne@xxxxxxxxxx>
    * Date: Tue, 26 Aug 2008 14:22:37 -0700

On Tue, 26 Aug 2008 12:27:09 -0400
Frank Kotler <fbkotler@xxxxxxxxxxx> wrote:

    gdb is a lot happier if we include debugging
    symbols - "-g", or better yet "-Fdwarf".


Indeed so, but, with only a trivial effort, gdb can also be made more
asm friendly, even for programs which do not have debug info. All one
has to do is to add some command sequences to ".gdbinit" in ones
home directory. [Note the leading dot].

Since I use gdb with both 32 and 64 bit programs, I picked a naming
convention which includes the bit size. Thus, I define:
a32i execute the next instruction
a32n same as a32i but skip over subroutines
a32r display registers without executing an instruction.

and the equivalent a64 commands (a64i, a64n, and a64r).

For example:

(gdb) a32n
_start () at test32.asm:10
10 mov eax,0
0x8048109 <_start+9>: mov eax,0x0
eax=0x001b1deb ebx=0xffff002b ecx=0xffcbca74 edx=0x001a93d0
esi=0xffcbca7c edi=0x08048100 ebp=0x00000000 esp=0xffcbca70

The definition of a32 is:

define a32n
ni
x /i $pc
printf "eax=0x%.8x ebx=0x%.8x ecx=0x%.8x edx=0x%.8x\n",$eax,$ebx,$ecx,$edx
printf "esi=0x%.8x edi=0x%.8x ebp=0x%.8x esp=0x%.8x\n",$esi,$edi,$ebp,$esp
end

[Note that you may have to unwrap the printf lines]

a32i is identical except ni is replaced with si, and a32r omits the
ni/si line.

The a64 definitions follow the same pattern, but require four printf
lines to display all of the 64-bit registers.

--
Chuck
http://www.pacificsites.com/~ccrayne/charles.html
------------------------------------------------------------

Okay... it wasn't exactly the one I was looking for (can't believe I didn't save that when he posted it!), and it isn't exactly what you want, S Mahen, but permit me to post it here, since I've found it. Although Chuck's gone, his web page still works, and there are some gems on there!

As for "ddd", if that's what you want to use, install the libraries that it wants. I thought "termcaps" was a very common one, but perhaps not. Looks like "ddd"'s "./configure" is looking for other things, too. As dogman says, just keep resolving dependencies until done. It's a PITA! Why does "ddd" need "connect" and "gethostbyname"? Planning to phone home? I may just be paranoid, but that looks weird to me. Well, the source is there, if you're worried about it.

Lessee... FEDORA's RedHat, right? So it probably uses the "rpm" package management system? I don't know how to use that. Never even got Nasm to install from an .rpm. SlackWare was not an early adopter. :) That may help you if you can learn how to use it...

As for other debuggers, there's "minibug" in that package from Jeff Owens. I think there's "asmbug" too, but I can't get that to work at the moment.

What I actually use, in most cases, is something from Terry Loveall. He pasted some Nasm code into Patrick Alken's "ald" to give it the "a"(ssemble) capability - just like DEBUG! :)  His page seems to have disappeared, so I put it here:
http://home.myfairpoint.net/fbkotler/debug-0.0.21.tgz

"Like DEBUG" is pretty faint praise, but I'm used to it. I suggest you learn to use gdb, with or without a "front end" - I think it's a more powerful debugger.

Best,
Frank