NASM Forum > Using NASM

How To do a loop in NASM?

(1/3) > >>

hello everyone I'm quite new to NASM and assembly in general. I've learned how to do the simple stuff such as adding and subtracting 2 numbers. I've been trying to write a program that computes the power of a number F(X)^n but for the life of me I can't figure it out. I know I need a loop for this but I don't know the command I need.
I would really appreciate a program I can play with and please keep in mind I'm a complete noobie and my only experience comes from C++ and a bit of python so I have no idea what I'm doing most of the time. thank you very much in advance and sorry for the poor English.

A small example to compute 2^5 using loop in 16-bit x86 code is:

--- Code: ---        mov     CX,5         ; number of times to loop
        mov     BX,2         ; multiplier
        mov     AX,BX       ; initial value
        mul     AX,BX        ; multiply AX by BX
        loop    label          ; loop decrements CX and if CX does not reach 0, jumps to specified label
                                ; otherwise, executes the next instruction.

--- End code ---

So basically the LOOP instruction executes the code within the loop CX numbers of times

There are loop instructions which loop according to some flags, and you can use conditional jumps to loop as well, but I'll let you read up a little rather than do it all for you :)

thank you for your reply. can you please link me some sources to read up on about loops?
and how do I compile this on Nasm to try it out? would these 2 lines work? (nasm -f elf32 example1.asm -o example1.o
ld -m elf_i386 example1.o -o example1)
what frustrates me about Assembly language is that it's syntax differs according to the environment it's running on.

also when I tried to compile the bit of code you gave me I was surprisingly greeted with an error on line 8 which is the multiplication line
the error is: ex4.asm:8: error: invalid combination of opcode and operands

Frank Kotler:
Hi CaRooR,

Welcome to the forum.

Let me paste this in while I've got it in the buffer...

Debs is a Nasm developer too. She and Yuri and I put a lot of work into that instruction set reference. Then it was removed from the Nasm doc. Despite being obsolete, it might be some use  to you.

Debs forgets that "mul" always uses ax or eax as the first parameter and complains if we tell it so... as you discovered... "imul" which is similar. will let us specify parameters/

You probably want

--- Code: ---nasm -f elf32 myprog.asm
ld -m elf_ i386 -o myprog myprog.o

global _start
section .text
 mov eax, 2 ; first param
mov ebx, 2 ; second param
mov ecx, 4 ; loop count
mul ebx
loop top ; the mysterious loop instruction!
mov ebx, eax; make the result the exit code
mov eax, 0 ; sys_exit
int 80h

--- End code ---

That's untested (when will I Learn?)

if it works. typing "echo $?" should show the result...  only up to a byte...

I may be back with some tested code... maybe...



[0] Message Index

[#] Next page

Go to full version