Support LQ: Use code LQ3 and save \$3 on Domain Registration
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Simple mathmatics written in Assembly code using nasm
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 01-19-2007, 11:52 AM #1 amon Member   Registered: May 2004 Location: UK Distribution: Debian, Ubantu, CentOS Posts: 146 Rep: Simple mathmatics written in Assembly code using nasm Hi I have had a go at a tutorial I got in a linux format magazine 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 3.5 hours of googling) have been helo world tutorials, tutorials that look confusingly different from nasm and 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'm using nasm assembler under Ubuntu Linux
 01-19-2007, 01:49 PM #2 95se Member   Registered: Apr 2002 Location: Windsor, ON, CA Distribution: Ubuntu Posts: 740 Rep: ecx does not contain the output your looking for. ecx contains a 32 bit integer, 50 specifically, but write needs a pointer to an array of characters. You'll just get a segmentation fault. You need to convert that integer to a string first. The easiest way to do what your doing is to link w/ the C library. That way you can use itoa, or scanf and printf. Creating functions to convert an integer to a string and back in assembly is not difficult, but it is beyond the level your looking for. In my assembly class a while ago, our teacher just created an include file w/ macros to run the C functions for you. This worked well since it allowed the class to create non-trivial examples w/o worrying about I/O.
 01-19-2007, 02:09 PM #3 indienick Senior Member   Registered: Dec 2005 Location: London, ON, Canada Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD Posts: 1,853 Rep: While this doesn't directly answer your question, if you're trying to learn assembler, I suggest starting with HLA (High Level Assembly). I've been spending a bit of my free time learning it, and it's nothing more than x86 assembly language with C-like syntax.
 01-24-2007, 01:31 PM #4 amon Member   Registered: May 2004 Location: UK Distribution: Debian, Ubantu, CentOS Posts: 146 Original Poster Rep: Thanks for the pointers. I figured that there was an issue with trying to output an integer as text. The rest looked like it was working. I have also found (for others looking to learn assembly) a website with a forum for flat assembler (which is similar to nasm). This has some useful resources. Just in case someone has the same situation as me my recommendation is to get both the pdf and the example code file from this site: PC Assembly Tutorial That covers all the basics and more. An excellent tutorial. 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 cheat on their homework (Which seems to be what most people asking many of the questions I have been googling are after).