flat assembler
Message board for the users of flat assembler.

Index > Linux > Simple mathmatics written in Assembly code using nasm

Author
Thread Post new topic Reply to topic
DavidHalliday



Joined: 19 Jan 2007
Posts: 3
DavidHalliday 19 Jan 2007, 17:25
I'm aware that I'm using nasm http://nasm.sourceforge.net/ assembler (differnt from most people here) but this seemed like the best forum I can find. I'm running Ubuntu Linux

I have had a go at a tutorial I got in a linux format magazine http://www.linuxformat.co.uk/ and I have found a similar tutorial on this site:
http://docs.cs.up.ac.za/programming/asm/derick_tut/

I'm haveing difficulty with adapting this to do something with numbers.
In short all I want to do is make a simple assembly example (for teaching) that does
the following:
1: takes two numbers input (optional as I'm guessing this is complex?)
2: adds them together
3: Displays the result

I've had a go at it but without luck yet. Can anyone tell me if i'm
making some stupid/simple mistake or give me a pointer to a website
with a simple solution. So far all I have found (in 4 hours of
googling) have been:
helo world tutorials (got that working good Smile )
tutorials that look confusingly different from nasm
examples that are too complex for my level.
And i found the nasm manual too technicxal for my relative beginner status

Any help would be much apreciated.

Here is my code:
Code:
section .data

section .text
       global _start

_start:
       mov eax, 5      ; set eax to 5
       mov ebx, 10     ; set ebx to 10
       mov ecx, 0      ; set ecx to 0

mathfunc:

       dec eax         ; decrease eax by 1
       add ecx, ebx    ; add ebx to ecx (store in ecx)

       cmp eax, 1      ; Compare eax with 1
       je output       ; If so, jump to output

       jmp mathfunc    ; And jump back to start of function

output:
       mov eax, 4      ; tell kernel to output
       mov ebx, 1      ; use standard output
                       ; ecx already contains output (i think)
       mov edx, 5      ; output length?
       int 80h

finish:
       mov eax, 1      ; Kernel 'exit prog' routine
       mov ebx, 0
       int 80h         ; Call kernel    


I should point out that I'm a teacher who has tried many things in the world of computers but this is my first dip into assembly. I'm aiming to use this as an example for some students who have to learn the basics of what assembly is for a course (and i figured learn by doing was the best option). This has proved far harder than I ever imagined... or I'm just having a day of stupidity.

Once I get this sorted I'll write up notes and get distributing them for others like me
Post 19 Jan 2007, 17:25
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 19 Jan 2007, 20:33
http://www.drpaulcarter.com/pcasm/

This covers NASM and 32-bit Pmode programming (Linux GCC, DJGPP, OpenWatcom, Borland, MSVC).
Post 19 Jan 2007, 20:33
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 19 Jan 2007, 22:28
for int80h/5, ECX should contain pointer to buffer, which holds data to write. in your case, ECX holds just some number (50?), not pointer to data.

to display numeric value, you must convert it to set of corresponding ascii characters (character '5' followed by character '0'), and then point ECX to them.

understand?
Post 19 Jan 2007, 22:28
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
DavidHalliday



Joined: 19 Jan 2007
Posts: 3
DavidHalliday 22 Jan 2007, 16:51
vid wrote:
for int80h/5, ECX should contain pointer to buffer, which holds data to write. in your case, ECX holds just some number (50?), not pointer to data.

to display numeric value, you must convert it to set of corresponding ascii characters (character '5' followed by character '0'), and then point ECX to them.

understand?

I was hopeing that there was an easy way to convert integer to text. the closest (easy/simple to demonstrate) thing I could find was manipulating hex values. But thanks for the help
Post 22 Jan 2007, 16:51
View user's profile Send private message Visit poster's website Reply with quote
DavidHalliday



Joined: 19 Jan 2007
Posts: 3
DavidHalliday 22 Jan 2007, 17:24
rugxulo wrote:
http://www.drpaulcarter.com/pcasm/

This covers NASM and 32-bit Pmode programming (Linux GCC, DJGPP, OpenWatcom, Borland, MSVC).

Thanks for that link It was just what I needed. I was able to use the library functions (and c code) to wrap arround my assembly which is perfect for the demmonstration I want to do. I'd love to learn to do assembly properly but I think that will have to be put asside. Most importantly I can teach my students more than/better than I was taught myself (which Is something I always aim to do) Before Friday I'd never tried to make assembly code work. My code still isn't perfect but I'm happy
Laughing

Just in case someone has the same situation as me my reccommendation is to get both the pdf and the example code file from this site:
http://www.drpaulcarter.com/pcasm/

If it helps anyone then here is my code etc:
Code:
%include "asm_io.inc"   ;library with input/output routines

section .data

section .text
        global asm_main

asm_main:
        mov eax, 5      ; set eax to 5
        mov ebx, 10     ; set ebx to 10
        mov ecx, 0      ; set ecx to 0

mathfunc:
        dec eax         ; decrease eax by 1
        add ecx, ebx    ; add ebx to ecx (store in ecx)

        cmp eax, 0      ; Compare eax with 1
        je output       ; If so, jump to output

        jmp mathfunc    ; And jump back to start of function

output:
        mov eax, ecx    ; eax is used to store the value output by print_int
        call print_int  ; output integer
        call print_nl   ; output new line

finish:
        mov eax, 1      ; Kernel 'exit prog' routine
        mov ebx, 0
        int 80h         ; Call kernel
    

Using the example code file I was able to get my code (which I had to modify slightly) working using the following instructions (you may have to check the order):
Code:
gcc -c driver.c
nasm -f elf MYCODE.asm
gcc -o OUTPUT.FILE driver.o MYCODE.o asm_io.o
    

If you change the MYCODE.asm file then you have to do the last two steps to make the changes to the output.

Hope this helps others... and not just to cheet on their homework (Which seems to be what most people asking many of the questions I have been googling are after).
Post 22 Jan 2007, 17:24
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 22 Jan 2007, 19:10
you might be interested in my project FASMLIB. It is portable library for 32bit assembler. Something like functions in asm_io.inc, but more advanced, with little better API etc.

Also, functions are called exactly same way on linux and windows, and code using my library is portable over these OSes.

If you are interested, mail me at vid (at) inamail (dot) sk, i can explain details and usage to you. or even better, contact me with ICQ.
Post 22 Jan 2007, 19:10
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.