Registers 32-bit vs 16-bit vs 8-bit, when to use which?

Started by brainfreaked, March 23, 2010, 09:28:47 PM

Previous topic - Next topic

brainfreaked

Hi all,

First of all, I'm as noob as noobs come but I'm trying to fix that so don't hold it against me.  :bg 

Anyhow, my question is about using registers and how to use them properly.  Sometimes I see people use EAX, and then sometimes just AX, and other times AH and HL.  If you're programming in Win32 wouldn't it be more efficient to always use EAX since the CPU addresses memory 32 bits at a time?   The only situation that I'm aware of for using a byte in a register is when comparing bytes from a file.  What other situations or reasons are there to using the lower bytes of a register (AH, AL, AX)?  Thank you.

Slugsnack

one situation i can immediately think of is when using lparam/wparam in winapi calls. often the upper and lower byte represent different things. in this case we can simply move one value to ah and another to al

brainfreaked

Thanks for your response.  In the case that you mentioned, is it really efficient for windows to use lparam/wparam like that?  Wouldn't it be more efficient if these values were stored as 32-bit?

oex

Use the best tool for the job!

It's a conceptual thing, kinda like commenting your code without making a mess.... You can see immediately what is going where and what size it is.... Things are only 'more efficient' if they take up more than a billionth of a second on your cpu :lol

Like branding on 3 frozen turkeys.... You might be eating a whole turkey on thanksgiving but knowing the weights from the labels tells you how long it will take you to shed the pounds afterwards

Also like 3 types of deodrant.... they all make you smell better but one might be better at attracting a mate, another makes you smell dignified and another is super strength BO bashing.... You can wear any one when playing sport, going out on a date or visiting your grandma at the old people's home but for best results read the label :wink

When you've been coding for hours and your fingers hurt you can save about 10-20% typing adding up to 8 years to your life.... better than giving up smoking!
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

hutch--

brainfreaked,

The basic are use the native data size that the operating system uses. Currently if you are creating 32 bit code, use the 32 bit registers where possible as it better fits how the processor works. You use 16 and 8 bit registers for dealing with data of that size but for counters and similar user defined code always use 32 bit registers. For addressing you don't have any other option.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

brainfreaked

I see.  I guess my concern is that it would take more CPU instructions to complete processing of a one or two byte as oppose to a dword.  By the way, how can I go about finding out how many bytes a CPU instruction takes?  For example with MOV, I believe it takes 3 bytes, but how's that determined?  Thanks.

brainfreaked

Quote from: brainfreaked on March 23, 2010, 11:45:19 PM
I see.  I guess my concern is that it would take more CPU instructions to complete processing of a one or two byte as oppose to a dword.  By the way, how can I go about finding out how many bytes a CPU instruction takes?  For example with MOV, I believe it takes 3 bytes, but how's that determined?  Thanks.

Thank you, this clears up a lot of my confusion. 

hutch--

To get the size of instructions you can check the Intel manuals for each instruction or cheat and disassemble some working code and look at the hex for each instruction. You will find it is of little use to you and it is more profitable to choose instructions on the basis of what they do rather than their byte size.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php