Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Meaning of unroll: A general programming question

Status
Not open for further replies.

DigiTan

New Member
A few months ago, I came across this this webpage that explains how to do arithmatic in the RISC archetecture (Z80). In each code examples, the author refers to something called "unrolling." What does this term mean exactly? I'm guessing from the context that he mean I should repeat these intructions multiple times (maybe use cut & paste or a macro). I tested this with a division routine but it didn't go well, maybe I just set it up wrong.
 
i looked at the webpage
he seems to use the term only for the size of the dividend in bits
dont know what he means??
but i have done division by repeated subtraction..
when you dont care/or want a remainder..
 
DigiTan said:
that explains how to do arithmatic in the RISC archetecture (Z80).

I've no idea what he means by 'unroll' either, but the Z80 certainly isn't a RISC (Reduced Instruction Set Chip), it's very much a CISC (Complex Instruction Set Chip).
 
"Unroll" is a term used to describe removing a loop and substituting straight line code. Sometimes, when the body of the loop does very little, such as shifting a bit, the decrement and branch instructions slow things down too much. For example, if you were bit-banging SPI, and you wanted the highest possible speed, you could unroll your loop. Of course it may mean other things to other people.
 
Unrolling programs

Unrolling is the process of replacing an iterative subroutine with several 'copies' of that subroutine so that it need only be called once - this saves time in traveling to and from that subroutine.

Code:
1.1 Classic 8-bit * 8-bit Unsigned Integer Multiplication
Input: H = Multiplier, E = Multiplicand, L = 0, D = 0
Output: HL = Product 

	sla	h		; optimised 1st iteration
	jr	nc,$+3
	ld	l,e
------
	add	hl,hl		; unroll 7 times
	jr	nc,$+3		; ...
	add	hl,de		; ...
------
The bit of code (last three lines) between the '------' marks needs to be copied and pasted SIX more times such that it exists seven times, one after the other. There will then be a total of EIGHT "iterations", one for each bit of the eight bit multiplier.
The first iteration can be optimised to take advantage of the fact that, on entry, the L register will be zero. This saves a few machine cycles.

The code could be optimised for compactness instead of speed by keeping the 'unrollable' section of code as a subroutine and calling it seven times, this may save a few bytes of ROM space at the cost of running speed.

The Z80 is a CISC, as Nigel said though (my personal favourite uprocessor :wink: ).
 
Okay, I'll use the cut & paste method. Hopefully, that'll get the project rolling again (er, bad pun). That's true about the Z80 being a CISC--with instructions being tokenized as they are. Anyway, I'm hoping to port some of that code over to an AVR.
 
Some of the PIC 18F series use hw multiply too --- i know the 18F452 has a built in 8x8 multiplier...
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top