0

This is a RISC code I came across in a book. The code is to multiply 10 with 5.

Mov ax,0
Mov bx,10
Mov cx,5
begin : Add ax,bx
        loop begin

It is given that total number of cycles taken by this code is 13 cycles. I don't understand how begin-loop begin loop gets terminated. We are not setting any condition for that. Can anyone please explain how is it interpreted.

Jonas
  • 121,568
  • 97
  • 310
  • 388
nomorequestions
  • 589
  • 2
  • 6
  • 20
  • 1
    _"This is a RISC code I came across"_ Are you sure? It looks a lot like x86 assembly. Refer to Volume 2 of the [Intel Software Developer Manual](http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html) for info on how `loop` works. – Michael Mar 04 '14 at 14:42
  • basically a duplicate of [How exactly does the x86 LOOP instruction work?](https://stackoverflow.com/q/46881279), except for not realizing the ISA. – Peter Cordes Jan 04 '21 at 12:24

2 Answers2

2

Most likely the idea is that the loop instruction will decrement cx and only return to begin if cx is non-zero. It looks like this supposed RISC was modelled on x86, which is pretty funny because x86 is not only CISC, but aggressively so.

al45tair
  • 4,405
  • 23
  • 30
1

As has been mentioned before, the code looks like it's x86 assembler rather than a RISC instruction set. In x86 the loop instruction decrements CX and branches if the value is not zero. If you're familiar with C it would look some like this:

int ax = 0;
int bx = 10;
int cx = 5;

do
{
  ax += bx;
}while (--cx !=0);
Sean
  • 60,939
  • 11
  • 97
  • 136