What is faster: stack or registers?

x86 / x64 programming
User avatar
madaxe
Posts: 8
Joined: Mon Apr 27, 2020 4:59 pm

What is faster: stack or registers?

Post by madaxe » Thu Jun 04, 2020 4:58 pm

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

User avatar
akuyou
Posts: 411
Joined: Mon Apr 22, 2019 3:19 am
Contact:

Re: What is faster: stack or registers?

Post by akuyou » Sun Jun 07, 2020 12:19 am

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.
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

User avatar
madaxe
Posts: 8
Joined: Mon Apr 27, 2020 4:59 pm

Re: What is faster: stack or registers?

Post by madaxe » 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

Peter Swinkels
Posts: 10
Joined: Sat May 16, 2020 11:54 am
Location: The Netherlands
Contact:

Re: What is faster: stack or registers?

Post by Peter Swinkels » Mon Jun 08, 2020 9:16 am

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.
A long time software developer. https://github.com/PeterSwinkels

User avatar
madaxe
Posts: 8
Joined: Mon Apr 27, 2020 4:59 pm

Re: What is faster: stack or registers?

Post by madaxe » 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 :) .

Peter Swinkels
Posts: 10
Joined: Sat May 16, 2020 11:54 am
Location: The Netherlands
Contact:

Re: What is faster: stack or registers?

Post by Peter Swinkels » Tue Jun 09, 2020 12:56 pm

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?
A long time software developer. https://github.com/PeterSwinkels

User avatar
madaxe
Posts: 8
Joined: Mon Apr 27, 2020 4:59 pm

Re: What is faster: stack or registers?

Post by madaxe » Wed Jun 10, 2020 10:47 am

Peter Swinkels wrote:
Tue Jun 09, 2020 12:56 pm
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?
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?

Peter Swinkels
Posts: 10
Joined: Sat May 16, 2020 11:54 am
Location: The Netherlands
Contact:

Re: What is faster: stack or registers?

Post by Peter Swinkels » Wed Jun 10, 2020 12:16 pm

madaxe wrote:
Wed Jun 10, 2020 10:47 am
Peter Swinkels wrote:
Tue Jun 09, 2020 12:56 pm
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?
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?
A long time software developer. https://github.com/PeterSwinkels

User avatar
madaxe
Posts: 8
Joined: Mon Apr 27, 2020 4:59 pm

Re: What is faster: stack or registers?

Post by madaxe » Thu Jun 11, 2020 1:23 pm

Peter Swinkels wrote:
Wed Jun 10, 2020 12:16 pm
I 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/

Peter Swinkels
Posts: 10
Joined: Sat May 16, 2020 11:54 am
Location: The Netherlands
Contact:

Re: What is faster: stack or registers?

Post by Peter Swinkels » Thu Jun 11, 2020 3:45 pm

madaxe wrote:
Thu Jun 11, 2020 1:23 pm
Peter Swinkels wrote:
Wed Jun 10, 2020 12:16 pm
I 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?
A long time software developer. https://github.com/PeterSwinkels

Post Reply

Return to “8086 Assembly Programming”