Page 1 of 2

What is faster: stack or registers?

Posted: Thu Jun 04, 2020 4:58 pm
by madaxe
Hi everyone;

I would like to know what is faster: preserve my register in the stack or use another register?

Stack:

Code: Select all

	mov cx,16
loop1:
	push cx
	mov cx,20
loop2:
	; do my stuff here

	loop loop2

	pop cx
	loop loop1
Register:

Code: Select all

	mov dx,16
loop1:
	mov cx,20
loop2:
	; do my stuff here

	loop loop2

	dec dx
	jnz loop1
Thanks in advance,
MadAxe

Re: What is faster: stack or registers?

Posted: Sun Jun 07, 2020 12:19 am
by akuyou
Registers are going to be fastest, and you should try and use them as much as possible

The stack is basically using the main memory... and if you think about the structure of the machine, the 'Registers' are built into the CPU... but the 'memory' is further away on the address bus (I know that's over simplifying, but you know what I mean)

You can check the speed of commands here - Appendix E
230985-001_80386_Programmers_Reference_Manual_1986.pdf
https://archive.org/details/bitsavers_i ... 6_27457025

According to that manual... a PUSH command takes 5 clocks... a POP takes another 5
a DEC takes just 2...

Of course,with a simple program on a vaguely modern processor, the speed isn't going be a big problem - but your question is a good one, as it's always best to try to be as efficient as possible.

Re: What is faster: stack or registers?

Posted: Sun Jun 07, 2020 2:27 pm
by madaxe
Hi;

Thank you very much for your great help and nice explanation :)

I have made a little shooter for Windows, Amiga, Atari and Spectrum and now I'm converting it to MS-Dos using assembly, so efficiency is very needed. You can check my little game here: http://sardonic.planetaclix.pt/

By the way, what is faster, loop or dec and jnz?

loop:

Code: Select all

	mov cx,16
loop1:
	; my stuff here

	loop loop1
Dec and jnz:

Code: Select all

	mov cx,16
loop1:
	; my stuff here

	dec cx
	jnz loop1
Thanks,
José Mário aka MadAxe

Re: What is faster: stack or registers?

Posted: Mon Jun 08, 2020 9:16 am
by Peter Swinkels
madaxe wrote: Sun Jun 07, 2020 2:27 pm Hi;

Thank you very much for your great help and nice explanation :)

I have made a little shooter for Windows, Amiga, Atari and Spectrum and now I'm converting it to MS-Dos using assembly, so efficiency is very needed. You can check my little game here: http://sardonic.planetaclix.pt/

By the way, what is faster, loop or dec and jnz?

loop:

Code: Select all

	mov cx,16
loop1:
	; my stuff here

	loop loop1
Dec and jnz:

Code: Select all

	mov cx,16
loop1:
	; my stuff here

	dec cx
	jnz loop1
Thanks,
José Mário aka MadAxe
I see "dec cx" followed by a JNZ instruction. I believe the LOOPNZ does both at once. Just a tip. Wikipedia has a fairly decent overview you may want to look into at https://en.wikipedia.org/wiki/X86_instr ... structions

Also, I have an ancient DOS program called Helppc which has some good reference info lying around. Let me know if you would like a copy.

Re: What is faster: stack or registers?

Posted: Mon Jun 08, 2020 4:28 pm
by madaxe
Peter Swinkels wrote: Mon Jun 08, 2020 9:16 am I see "dec cx" followed by a JNZ instruction. I believe the LOOPNZ does both at once. Just a tip. Wikipedia has a fairly decent overview you may want to look into at https://en.wikipedia.org/wiki/X86_instr ... structions
Yeap, It really makes sense, thanks.
Peter Swinkels wrote: Mon Jun 08, 2020 9:16 am Also, I have an ancient DOS program called Helppc which has some good reference info lying around. Let me know if you would like a copy.
Yes,I would like to have HelpPC :) .

Re: What is faster: stack or registers?

Posted: Tue Jun 09, 2020 12:56 pm
by Peter Swinkels
madaxe wrote: Mon Jun 08, 2020 4:28 pm
Peter Swinkels wrote: Mon Jun 08, 2020 9:16 am I see "dec cx" followed by a JNZ instruction. I believe the LOOPNZ does both at once. Just a tip. Wikipedia has a fairly decent overview you may want to look into at https://en.wikipedia.org/wiki/X86_instr ... structions
Yeap, It really makes sense, thanks.
Peter Swinkels wrote: Mon Jun 08, 2020 9:16 am Also, I have an ancient DOS program called Helppc which has some good reference info lying around. Let me know if you would like a copy.
Yes,I would like to have HelpPC :) .
I did a bit of searching: you should be able to find a copy by typing "helppc abandonware" in Google. Let me know how it turns out, okay?

Re: What is faster: stack or registers?

Posted: Wed Jun 10, 2020 10:47 am
by madaxe
Peter Swinkels wrote: Tue Jun 09, 2020 12:56 pmI did a bit of searching: you should be able to find a copy by typing "helppc abandonware" in Google. Let me know how it turns out, okay?
I have downloaded HelpPC 2.10 and in fact I remember using this very useful tool in the early 90s. Good memories :)

Well, if I didn't get it wrong and according 8086 timings here it is:

lOOP takes 18 clocks when jumps and 5 clocks when no jumps. Bytes size is 2.

DEC takes 2 clocks and Bytes size is 1.

JNZ takes 16 clocks when jumps and 4 clocks when no jumps. Bytes size is 2.

So, DEC+JNZ takes 18 clocks when jump and 6 clocks when no jump and Bytes size is 3.

But according to the Cheat Sheet provided by chibi lOOP takes only 17 clocks when jumps and 5 clocks when no jumps.

It seems it's better to use LOOP instead DEC+JNZ. What do you think?

Re: What is faster: stack or registers?

Posted: Wed Jun 10, 2020 12:16 pm
by Peter Swinkels
madaxe wrote: Wed Jun 10, 2020 10:47 am
Peter Swinkels wrote: Tue Jun 09, 2020 12:56 pmI did a bit of searching: you should be able to find a copy by typing "helppc abandonware" in Google. Let me know how it turns out, okay?
I have downloaded HelpPC 2.10 and in fact I remember using this very useful tool in the early 90s. Good memories :)

Well, if I didn't get it wrong and according 8086 timings here it is:

lOOP takes 18 clocks when jumps and 5 clocks when no jumps. Bytes size is 2.

DEC takes 2 clocks and Bytes size is 1.

JNZ takes 16 clocks when jumps and 4 clocks when no jumps. Bytes size is 2.

So, DEC+JNZ takes 18 clocks when jump and 6 clocks when no jump and Bytes size is 3.

But according to the Cheat Sheet provided by chibi lOOP takes only 17 clocks when jumps and 5 clocks when no jumps.

It seems it's better to use LOOP instead DEC+JNZ. What do you think?
I have no idea. Run tests and see for yourself. What are you writing and what is the intended platform?

Re: What is faster: stack or registers?

Posted: Thu Jun 11, 2020 1:23 pm
by madaxe
Peter Swinkels wrote: Wed Jun 10, 2020 12:16 pmI have no idea. Run tests and see for yourself. What are you writing and what is the intended platform?
Hehehe, I'm writing nothing useful, just a simple shooter.
I've made a shoot'm'up for Windows, Amiga, Atari and Speccy and now I'm converting it to MS-DOS, VGA and SB using C and assembly for graphical stuff.
I have already coded all the routines in assembly like printing and moving sprites and double buffer stuff.
And now I'm making all the optimizations and turning all the assembly code more efficient.
You can check my little shooter here: http://sardonic.planetaclix.pt/

Re: What is faster: stack or registers?

Posted: Thu Jun 11, 2020 3:45 pm
by Peter Swinkels
madaxe wrote: Thu Jun 11, 2020 1:23 pm
Peter Swinkels wrote: Wed Jun 10, 2020 12:16 pmI have no idea. Run tests and see for yourself. What are you writing and what is the intended platform?
Hehehe, I'm writing nothing useful, just a simple shooter.
I've made a shoot'm'up for Windows, Amiga, Atari and Speccy and now I'm converting it to MS-DOS, VGA and SB using C and assembly for graphical stuff.
I have already coded all the routines in assembly like printing and moving sprites and double buffer stuff.
And now I'm making all the optimizations and turning all the assembly code more efficient.
You can check my little shooter here: http://sardonic.planetaclix.pt/
I downloaded the Windows version. The screen briefly goes black and then the program returns to Windows. What gives?