Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Assembly in Ubuntu Linux using NASM help???  (Read 4548 times)

0 Members and 1 Guest are viewing this topic.

timtim41

    Topic Starter


    Beginner
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Linux variant
Assembly in Ubuntu Linux using NASM help???
« on: March 15, 2011, 03:33:21 PM »
Hi. I'm pretty new to programming in 32-bit assembly. I'm trying to make a basic Min-Max game in assembly using the NASM assembler. The only trouble I'm having is getting integer input from the user. I cannot find anywhere on the internet a simple example of user input using NASM in linux. Any help is GREATLY appreciated. Thank you :)
T Jewell

Geek-9pm



    Genius
  • Geek After Dark
  • Thanked: 575
    • Geek 9pm blog is of line
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows XP
Re: Assembly in Ubuntu Linux using NASM help???
« Reply #1 on: March 15, 2011, 03:55:25 PM »
You want integer input?

Create a procedure that converts  ASCII to a digit in the range of 0-9, if out of range set a flag.
Like this:
    sub ax, '0'     ; if out of range, sets carry

Create a procedure that gets a single char from the console input in Linux.

Create a procedure that calls the above and multiples  prior value by ten and adds to next value. Exit on error.

Finally have a procedure that uses the above. Check to see if user hit CR or a invalid digit. If CR all done, else chide user and start over.

You may want a cuter to limit integer to five digits.

Glad to see somebody using NASM.

Netwide Assembler
http://en.wikipedia.org/wiki/Netwide_Assembler

timtim41

    Topic Starter


    Beginner
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Linux variant
Re: Assembly in Ubuntu Linux using NASM help???
« Reply #2 on: March 16, 2011, 05:03:52 PM »
Thanks for the advice, ill try that. But I actually don't know how to get input of any kind yet. I've tried looking up the system calls and interrupts for using NASM with Linux, but I'm having trouble understanding it unfortunately. But I'll keep on trying and let you know where I'm still stuck at or if I figure it out. :)
T Jewell

Geek-9pm



    Genius
  • Geek After Dark
  • Thanked: 575
    • Geek 9pm blog is of line
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows XP
Re: Assembly in Ubuntu Linux using NASM help???
« Reply #3 on: March 17, 2011, 02:16:59 PM »
Looking around, I found this item.
From a WordPress Blog,  February 13, 2008 by taufanlubis
Quote
Simple tutorial by example. Shows how to do somethoing  in Linux using the Int 80H feature.
Taufan Lubis Ė Ubuntu Linux[

Programming Language Ė NASM (Assembly language in Linux)

February 13, 2008 by taufanlubis

At about 22 years ago, Assembly Language is my second language after BASICA. I used Borland Turbo Assembler and Microsoft Debug on that time. I think Debug one of the very powerful tools for hacking. I like hacking. With Assembly, you can order the machine to do anything you want and go directly to every ports in the machine.

I just figure out, is there any Assembly Language in Linux?

Because, usually Assembly Language use DOS Services. Meanwhile, there is no DOS in Linux.

I finally found a good application, named NASM (Net wide Assembler). Itís a Assembly Language compiler in Linux. Iím still new with NASM, so I just take the sample codes from http://leto.net/writing/nasm.php.

NASM doesnít have a text editor, so you have to type your code using other text editor then save the file with .asm file extension.
....
Hope to post more on this topic when I find the exact code your need.

timtim41

    Topic Starter


    Beginner
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Linux variant
Re: Assembly in Ubuntu Linux using NASM help???
« Reply #4 on: March 21, 2011, 02:08:07 PM »
Alright cool! Thanks a lot, man. Yeah that is probably the reason why it is so hard to find enough stuff about the NASM compiler when mostly every assembly tutorial on the web is using MASM or something with DOS, but thanks for the link and everything, I'll definitely check it out.
T Jewell

Geek-9pm



    Genius
  • Geek After Dark
  • Thanked: 575
    • Geek 9pm blog is of line
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows XP
Re: Assembly in Ubuntu Linux using NASM help???
« Reply #5 on: March 21, 2011, 08:05:09 PM »
Found this book:
Assembly Language Step-by-Step with Linux 3rd Edition
 © Jeff Duntemann, Wiley Publishing, Inc.


It is an excellent source of information on programming assembly language for a Linux.
There is a book written by Jeff Duntemann and it is now in its third edition. The third edition is a significant rewrite. Much of the book focuses now on Linux and no longer on DOS programming. That was an excellent move, all you can do in DOS programming is just 16-bit executable files. Unless you're very clever. And I am not that clever.
Now the new edition of his book makes it possible for anybody to write 32-bit assembler language code in Linux. And it's not as hard as you might imagine. In fact, although the author is super qualified, he has chosen to make his book as easy as possible for everybody. He focuses on giving you the essential tools and then let you do the rest. Here is a quotation from his book.
Quote
The idea behind the book, nutty as it might seem, is to teach assembly language as your first programming language. No previous programming experience required. When I wrote the very first edition of the book way back in 1988, I thought it was a bit of a reach, but 175,000 copies and countless fan letters later, I guess it actually worked.
If you go to his website you can download the source code for some of the examples he gives in the book. Of course, if you want the book, you have to pay for it. But at least the source code is available for those of you curious and it gives you an idea of the both the simplicity and the quality of his work. Here's a bit of sample code.
Code: [Select]

;; Assembly Language Step-by-Step with Linux 3rd Edition
;;  © Jeff Duntemann, Wiley Publishing, Inc.
;;   http://www.duntemann.com/assembly.html

;  Executable name : EATSYSCALL
;  Version         : 1.0
;  Created date    : 1/7/2009
;  Last update     : 2/18/2009
;  Author          : Jeff Duntemann
;  Description     : A simple program in assembly for Linux, using NASM 2.05,
;    demonstrating the use of Linux INT 80H syscalls to display text.
;
;  Build using these commands:
;    nasm -f elf -g -F stabs eatsyscall.asm
;    ld -o eatsyscall eatsyscall.o
;

SECTION .data ; Section containing initialised data

EatMsg: db "Eat at Joe's!",10
EatLen: equ $-EatMsg

SECTION .bss ; Section containing uninitialized data

SECTION .text ; Section containing code

global _start ; Linker needs this to find the entry point!

_start:
nop ; This no-op keeps gdb happy...
mov eax,4 ; Specify sys_write  call
mov ebx,1 ; Specify File Descriptor 1: Standard Output
mov ecx,EatMsg ; Pass offset of the message
mov edx,EatLen ; Pass the length of the message
int 80H ; Make kernel call

MOV eax,1 ; Code for Exit Syscall
mov ebx,0 ; Return a code of zero
int 80H ; Make kernel call
Later on, as there is enough interest in the subject, I may do a complete review of the book and post in  the reviews section here on computer Hope. It is possibly the best overall book on a single language programming for almost everybody. Programmers who are more experienced might like this book. But the real experienced programmers would already have available to them some references that they would get on-the-job.

And I am still looking for the answer to your question. I think it is in the book.
 Microphone off



timtim41

    Topic Starter


    Beginner
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Linux variant
Re: Assembly in Ubuntu Linux using NASM help???
« Reply #6 on: March 24, 2011, 02:49:34 PM »
Ok awesome! Ill check out the book for sure because there is not any other decent tutorial on the net that I can find anywhere so Ill be sure to check it out. I do already know how to print text and do math with 32-bit Linux assembly, but I just need help with getting input of any kind. I greatly appreciate your help and I look forward to any further comments or advice you have for me. Thank you :)
T Jewell

Geek-9pm



    Genius
  • Geek After Dark
  • Thanked: 575
    • Geek 9pm blog is of line
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows XP
Re: Assembly in Ubuntu Linux using NASM help???
« Reply #7 on: March 24, 2011, 08:44:38 PM »
After posting the code I noticed a problem with word wrap. For the assembler you can not have word wrap. The assembler sees the end of line as end of statement. That happens when I use Notepad with word wrap on.

timtim41

    Topic Starter


    Beginner
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Linux variant
Re: Assembly in Ubuntu Linux using NASM help???
« Reply #8 on: March 30, 2011, 05:19:19 PM »
I think I might have figured it out. I have been reading a pdf that a friend sent me and please correct me if I'm wrong, but in the BSS section you can reserve an input buffer such as "resd" for decimal or "resb" for bytes I believe. Also I found out that to GET the input you would do the following:

    section .data
           bufferSize: equ 1          ; or however big you want the buffer to be

    section .bss
           input1 resd bufferSize

    section .text
          global _start
    _start:
            mov eax, 4
            mov ebx, 1
            mov ecx,  input1
            mov edx, bufferSize
            int 0x80

            mov eax, 1
            mov ebx, 0
            int 0x80

And that SHOULD get input as a decimal, and not just a byte. I hope...
T Jewell

Geek-9pm



    Genius
  • Geek After Dark
  • Thanked: 575
    • Geek 9pm blog is of line
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows XP
Re: Assembly in Ubuntu Linux using NASM help???
« Reply #9 on: March 30, 2011, 08:38:19 PM »
Sorry, that will not do what you want.
Code: [Select]
      mov eax, 4      ; Specify sys_write
      mov ebx, 1      ; Specify File Descriptor

You want input, which is a read function.

3. sys_read
Syntax: ssize_t sys_read(unsigned int fd, char * buf, size_t count)
Source: fs/read_write.c
Action: read from a file descriptor


So change the 4 to a 3 to do input.
Code: [Select]
      mov eax, 3      ; Specify sys_read
      mov ebx, 1      ; Specify File Descriptor
It will literally read whatever is input.

EDIT: Some more information.
Quote
Linux System Call Table

The following table lists the system calls for the Linux 2.2 kernel. It could also be thought of as an API for the interface between user space and kernel space. My motivation for making this table was to make programming in assembly language easier when using only system calls and not the C library (for more information on this topic, go to http://www.linuxassembly.org). On the left are the numbers of the system calls. This number will be put in register %eax. On the right of the table are the types of values to be put into the remaining registers before calling the software interrupt 'int 0x80'. After each syscall, an integer is returned in %eax.

©2004, Gary L. Burt
http://bluemaster.iu.hio.no/edu/dark/lin-asm/syscalls.html
Click on link for full list, it is very complete.
« Last Edit: March 30, 2011, 08:53:44 PM by Geek-9pm »

Geek-9pm



    Genius
  • Geek After Dark
  • Thanked: 575
    • Geek 9pm blog is of line
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows XP
Re: Assembly in Ubuntu Linux using NASM help???
« Reply #10 on: April 06, 2011, 01:10:10 PM »
Well, it looks like I've been off more than I can chew. I'll try my best to keep my promise, let's chew it over bit by bit and byte by byte. There is not much published on the Internet about assembly language tutorials and very little that is specific to Linux. There is some. What is needed here is a big program that does something useful and same time demonstrates the sort of things that are done in assembly language inside of Linux. Yes, we all know that Linux is based on C  and that most of the things you need already in the C  library. They original poster wants a specific answer in assembly language or Linux.
Hang on, some more  links show where you can get the materials to participate in this little project, if you wish. The program to be used here is from a book written by Jeff Duntemann.  The name of the program is uppercase2.asm and it runs under Ubuntu.

Links:
NASM - The Netwide Assembler is 80x86 assembler designed for portability and modularity.
http://linux.softpedia.com/get/Programming/Compilers/NASM-The-Netwide-Assembler-643.shtml

Ubuntu Netbook Edition (UNE), known as Ubuntu Netbook Remix prior to the release of ...is a version of the Ubuntu Linux distribution that has been optimized to enable it to work better on netbooks and other devices with small screens or ...
http://en.wikipedia.org/wiki/Ubuntu_Netbook_Edition
Ubuntu Netbook Edition

Source code for uppercaser2.asm
http://www.duntemann.com/
(code in next post)

Geek-9pm



    Genius
  • Geek After Dark
  • Thanked: 575
    • Geek 9pm blog is of line
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows XP
Re: Assembly in Ubuntu Linux using NASM help???
« Reply #11 on: April 06, 2011, 01:15:42 PM »
http://www.duntemann.com/
Source code for uppercaser2.asm

Code: [Select]
;  Executable name : uppercaser2
;  Version         : 1.0
;  Created date    : 3/25/2009
;  Last update     : 3/25/2009
;  Author          : Jeff Duntemann
;  Description     : A simple program in assembly for Linux, using NASM 2.05,
;    demonstrating simple text file I/O (through redirection) for reading an
;    input file to a buffer in blocks, forcing lowercase characters to
;    uppercase, and writing the modified buffer to an output file.
;
;  Run it this way:
;    uppercaser2 > (output file) < (input file) 
;
;  Build using these commands:
;    nasm -f elf -g -F stabs uppercaser2.asm
;    ld -o uppercaser2 uppercaser2.o
;
SECTION .bss ; Section containing uninitialized data

BUFFLEN equ 1024 ; Length of buffer
Buff: resb BUFFLEN ; Text buffer itself

SECTION .data ; Section containing initialised data

SECTION .text ; Section containing code

global _start ; Linker needs this to find the entry point!

_start:
nop ; This no-op keeps gdb happy...

; Read a buffer full of text from stdin:
read:
mov eax,3 ; Specify sys_read call
mov ebx,0 ; Specify File Descriptor 0: Standard Input
mov ecx,Buff ; Pass offset of the buffer to read to
mov edx,BUFFLEN ; Pass number of bytes to read at one pass
int 80h ; Call sys_read to fill the buffer
mov esi,eax ; Copy sys_read return value for safekeeping
cmp eax,0 ; If eax=0, sys_read reached EOF on stdin
je Done ; Jump If Equal (to 0, from compare)

; Set up the registers for the process buffer step:
mov ecx,esi ; Place the number of bytes read into ecx
mov ebp,Buff ; Place address of buffer into ebp
dec ebp ; Adjust count to offset

; Go through the buffer and convert lowercase to uppercase characters:
Scan:
cmp byte [ebp+ecx],61h ; Test input char against lowercase 'a'
jb Next ; If below 'a' in ASCII, not lowercase
cmp byte [ebp+ecx],7Ah ; Test input char against lowercase 'z'
ja Next ; If above 'z' in ASCII, not lowercase
; At this point, we have a lowercase char
sub byte [ebp+ecx],20h ; Subtract 20h to give uppercase...
Next: dec ecx ; Decrement counter
jnz Scan ; If characters remain, loop back

; Write the buffer full of processed text to stdout:
Write:
mov eax,4 ; Specify sys_write call
mov ebx,1 ; Specify File Descriptor 1: Standard output
mov ecx,Buff ; Pass offset of the buffer
mov edx,esi ; Pass the # of bytes of data in the buffer
int 80h ; Make kernel call
jmp read ; Loop back and load another buffer full

; All done! Let's end this party:
Done:
mov eax,1 ; Code for Exit Syscall
mov ebx,0 ; Return a code of zero
int 80H ; Make kernel call

A later post will explain how to modify this code to suit the  request of the OP.

embs

  • Guest
Re: Assembly in Ubuntu Linux using NASM help???
« Reply #12 on: April 20, 2011, 02:12:11 PM »
Thanks for help! I'm also learning how to program in assembly using nasm and will keep looking this thread. For now, i'm searching for how to get decimal input from user too. Got some difficult codes and looking for some more intelligible algorithm. If i catch it, will post here. Thanks again!

Geek-9pm



    Genius
  • Geek After Dark
  • Thanked: 575
    • Geek 9pm blog is of line
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows XP
Re: Assembly in Ubuntu Linux using NASM help???
« Reply #13 on: April 21, 2011, 11:16:20 AM »
More about Linux assembly language .
Standard input. Standard output.

Instead of posting more code, I'm going to comment about code that has already been posted. The code I posted was from the book Assembly language step-by-step  programming with Linux, third edition. Yes, third edition really is different from his earlier works. It still is a teaching book ,an introduction to assembly language, but with much more attention to what you can do in Linux.
Now looking at the code earlier posted, notice there is an input routine that can be called the standard input device. Likewise the output routine is called the standard output device. These are handled by the kernel in the proper way so that input comes from the keyboard and the output goes to your display. Of course the display in this case is going to be a console or terminal in Linux. When a user types on the keyboard there's a number of things that happen at the hardware level, and we're just interested in getting the keystrokes in the ASCII format. The standard input takes care of this. Therefore, when the user hits he letter A the standard input will get the number 65. If it was the letter B. it will be 66 and so on. That is for uppercase. In the program posted you will note that he makes a test to see if the letters are in the range of lowers case. In the standard ASCII table the lower case letters are actually higher up. Therefore if the user typed an lowercase letter the program will subtract a certain value in order to force it to uppercase. After the entire buffer has been processed for as many characters that are actually in the buffer, it is sent to the standard output device and we will see uppercase letters where ever the user typed lowercase letters. This is a fairly good teaching exercise to introduce the ideas of standard input, standard output, use of the ASCII table and how to force a conversion of a character. The individual letters we call characters here or, using the C lingo, we call them chars, rhymes with cars.
Now why is it so important to know how to do standard input and standard output? Because this opens the door of using files instead of simply the console for input and output . In Linux you can do redirection so that input and output come from a file and go to file. This is one of the main points that he makes in the book . Once you've got that under your belt you can do lots of interesting things and create some very useful utilities that use the standard input and output. That way you don't have to deal with the issues of opening up a file and making sure the file exists and can be written to. The kernel will take care of all the error trapping and stop your program if there's an error with the file system.

Your original question was about how to get numbers into a memory location using Linux assembly language. I'll explain more about that in the next post. But first I wanted you and anybody else reading this understand why we have to learn the standard input and output in Linux. Once over that hurdle we can go on to do other things.

I had to do this with dictation, so there probably are some funny errors. I had to get back to work, it's my turn to mop the floor.
(microphone off)