Author Topic: Repeat bunch of instructions  (Read 14607 times)

Offline Dennis

  • Jr. Member
  • *
  • Posts: 2
Repeat bunch of instructions
« on: July 28, 2012, 08:08:16 PM »
Hello everyone,

In NASM I normally use "TIMES [TIMES] [INSTRUCTION]" to repeat one instruction several times.
But now I want to repeat a few instruction several times.
And by my knowledge "TIMES" can't do this.

This is basically what I wanna do:
Repeating ten times:
Code: [Select]
ADD EAX, EAX
CMP EAX, 10
JAE x

Putting this 10 times hardcoded in my sources gives the required execution result, but it also gives me 30 lines of code.
So I was wondering if there is a way to repeat a bunch of instructions several times in NASM.

Thanks in advance,
Dennis

Offline Gunner

  • Jr. Member
  • *
  • Posts: 74
  • Country: us
    • Gunners Software
Re: Repeat bunch of instructions
« Reply #1 on: July 28, 2012, 09:26:47 PM »
To repeat a set of instructions a certain amount of times in any language, you would use a loop.

Code: [Select]
mov ecx, 10
DoEAX:
add eax, eax
dec ecx
jnz DoEAX
« Last Edit: July 28, 2012, 09:31:47 PM by Gunner »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Repeat bunch of instructions
« Reply #2 on: July 28, 2012, 09:39:48 PM »
Code: [Select]
%rep 10
ADD EAX, EAX
CMP EAX, 10
JAE x
%endrep
Would do what you describe... but it would still give you 30 lines of code...
Code: [Select]
looptop:
ADD EAX, EAX
CMP EAX, 10
JB looptop
Might do what you want... but if eax were zero to start with, it would loop forever, and if non-zero it would "double" less than ten times before eax > 10.

I guess the closest equivalent to a multi-line "times" would be "%rep" but it doesn't make too much sense. What do you intend to do?

Best,
Frank

« Last Edit: July 28, 2012, 09:43:21 PM by Frank Kotler »

Offline Dennis

  • Jr. Member
  • *
  • Posts: 2
Re: Repeat bunch of instructions
« Reply #3 on: July 29, 2012, 11:28:51 AM »
Yes, "%rep" is what I was looking for.

I now see I was a bit unclear in my first post.
I wanted to unroll an (fixed size) 10 times loop, to save execution time. (Every JMP statement would take a few unneeded clock-cycles)
With this "30 lines of code" I meant lines of code in my source - it would make it more unclear than this 5 lines in the "%rep" bunch does.

Any comparisons of EAX and 0 are already done before the loop, so don't worry about infinite loops :P
(Which also couldn't occur with hard-coded loops - in the assembled file)

You helped me a lot.

Many thanks,
Dennis

Offline oldefoxx

  • Jr. Member
  • *
  • Posts: 3
Re: Repeat bunch of instructions
« Reply #4 on: September 04, 2012, 03:01:51 AM »
REp, REPE, REPNE, REPZ, and REPNZ are intended to work with string instructions, the contents of the (e)cx register, and the direction state (CLD or SED for going up or down in memory).  So is is not a choice for more complex operations.

Usually you would start with something like

  J(e)cxz skipover
domore:
   do your thing here
   dec (e)cx
   jnz domore
skipover:

Note my emphasis on (e)cx.  You have to decide if the 32-bits of the ecx register are needed,
or if you just want the 16 bits of cx.  Since you are doing only a count of 10, you could get
by with just using the upper or lower byte of any register, but the first jump would not be
applicable.  Instead, suppose you decided to use cl.  you could do "or cl,cl" or "and cl,cl" to
set a flag state related to cl's contents.   On the other hand, since you know you are starting
with 20, you can bypass the first jump altogether.  I won't swear that you can do an "or" or
an "and" involving cl because I haven't tried it.  Lots of operations are pretty specific to the
al, ah, ax, or eax registers, and far less applicable to the other registers.  Trial and Error tells
the tale. 

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Repeat bunch of instructions
« Reply #5 on: September 04, 2012, 05:33:36 AM »
You are quite correct that the "rep" prefix (and family) would not be useful here. What was being discussed was the preprocessor command "%rep". Read all about it here:
http://www.nasm.us/xdoc/2.10.04/html/nasmdoc4.html#section-4.5

While we're at it... the instruction to set the direction flag down is "std", not "sed"... :)

Best,
Frank