Author Topic: How To do a loop in NASM?  (Read 23722 times)

Offline CaRooR

  • Jr. Member
  • *
  • Posts: 5
How To do a loop in NASM?
« on: August 12, 2021, 05:35:45 PM »
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.

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 224
  • Country: gb
    • GPUZoo
Re: How To do a loop in NASM?
« Reply #1 on: August 12, 2021, 06:59:16 PM »
A small example to compute 2^5 using loop in 16-bit x86 code is:

Code: [Select]
        mov     CX,5         ; number of times to loop
        mov     BX,2         ; multiplier
        mov     AX,BX       ; initial value
label:
        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.

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 :)
« Last Edit: August 12, 2021, 07:01:04 PM by debs3759 »
My graphics card database: www.gpuzoo.com

Offline CaRooR

  • Jr. Member
  • *
  • Posts: 5
Re: How To do a loop in NASM?
« Reply #2 on: August 12, 2021, 07:30:21 PM »
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.

Offline CaRooR

  • Jr. Member
  • *
  • Posts: 5
Re: How To do a loop in NASM?
« Reply #3 on: August 12, 2021, 07:36:53 PM »
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

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How To do a loop in NASM?
« Reply #4 on: August 13, 2021, 12:21:35 AM »
Hi CaRooR,

Welcome to the forum.

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

http://home.myfairpoint.net/fbkotler/nasmdocr.html

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: [Select]
nasm -f elf32 myprog.asm
ld -m elf_ i386 -o myprog myprog.o

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

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...

Best,
Frank


Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 224
  • Country: gb
    • GPUZoo
Re: How To do a loop in NASM?
« Reply #5 on: August 13, 2021, 01:18:35 AM »
Thanks Frank. It's been a while since I wrote any code, so I had forgotten that :)
My graphics card database: www.gpuzoo.com

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 224
  • Country: gb
    • GPUZoo
Re: How To do a loop in NASM?
« Reply #6 on: August 13, 2021, 01:20:35 AM »
An earlier version of the nasm docs that gives more info about instructions is attached.
My graphics card database: www.gpuzoo.com

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How To do a loop in NASM?
« Reply #7 on: August 13, 2021, 01:24:27 AM »
Well that doesn't work... 1 in eax for sys_exit. not 0!

I think I'm all done.
Sorry I can't help you...

Best,
Frank

Offline CaRooR

  • Jr. Member
  • *
  • Posts: 5
Re: How To do a loop in NASM?
« Reply #8 on: August 13, 2021, 06:59:56 PM »
I'm sorry If I sound rude but to be honest reading from that manual is boring and it looks kind of stale. if you have any video sources that teach assembly for real beginners I'd be grateful.
but any ways thanks for the HUGE help here. I now know how to add, subtract AND multiply 2 numbers in assembly. I mean I'm starting to draw some parallels between assembly and C but assembly's code is much more complex than I thought.
« Last Edit: August 13, 2021, 07:04:12 PM by CaRooR »

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 224
  • Country: gb
    • GPUZoo
Re: How To do a loop in NASM?
« Reply #9 on: August 13, 2021, 07:33:45 PM »
I can't help with guides to learning assembly code, as I learned by reading CPU programmers guides from the CPU manufacturers, and by downloading open source and public domain apps which shared their code. That was all a long time ago, and as you can see from the mul code I posted above, I'm a little rusty :)
My graphics card database: www.gpuzoo.com

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: How To do a loop in NASM?
« Reply #10 on: August 13, 2021, 11:54:56 PM »
An example with loops. A little program which calculates the successor of an integer.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How To do a loop in NASM?
« Reply #11 on: August 14, 2021, 02:24:59 AM »
That's a gem, Frederico! Thank you!

Best,
Frank


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How To do a loop in NASM?
« Reply #12 on: August 15, 2021, 09:25:28 PM »
Quote
I'm sorry If I sound rude but to be honest reading from that manual is boring and it looks kind of stale. if you have any video sources that teach assembly for real beginners I'd be grateful.
Video? What would video show?
l
lo
loo
loop
Like  that?
With Debs screwing up "mul" and me screwing up "sys_exit" I can see we might need a new approach. but I don't see video helping. Maybe I don't know what you have in mind. Can you explain?

Best,
Frank


Offline CaRooR

  • Jr. Member
  • *
  • Posts: 5
Re: How To do a loop in NASM?
« Reply #13 on: August 16, 2021, 06:22:14 PM »
no I clearly meant someone writing code in assembly and teaching/explaining each bit of code they write so that noobies can learn and practice it themselves.

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 224
  • Country: gb
    • GPUZoo
Re: How To do a loop in NASM?
« Reply #14 on: August 16, 2021, 07:35:58 PM »
You might find my boot sector code useful in that case, as I fully comment everything I write (I want to be sure I understand what I write if I need to modify something 20 years down the line). My old CPUID code might be useful as well. asm and include files are in this rar file

To find an example of a loop, search for "Test_CT" or "KBC_Wait" in cpu_id.asm
« Last Edit: August 16, 2021, 07:48:33 PM by debs3759 »
My graphics card database: www.gpuzoo.com