🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

ASM variables

Started by
18 comments, last by Calin 4 years, 1 month ago

In the 32 bit assembler mode (protected mode) the variable size is dictated by the registers size (which in 32 bit mode is 32) i.e. like if you want to use large variables that will result in additional work (additions and other mathematical operation on the processor) and more code?

My project`s facebook page is “DreamLand Page”

Advertisement

On intel, no additional work is done.
Addition of 8bit, 32, 64, or even 256bit registers takes one single cycle of the processor(EDIT: operating on integers. For floating point operations, it is a little bit more complicated). With some exceptions. In the manuals of Intel you can read a long table with the costs of every single instruction in cycles.

(often instructions take 0.25 of a cycle, as the processors can execute various instructions in parallel on a SINGLE core(this is EXTRA parallelism APART of the parallelism created by the multiple cores))

The code that a programmer writes has the same length regardless the mode.
The size of the executable varies from one mode to another, but not the amount of code written by hand.
(you can use 32bit regs from 16bit mode)

EDIT: I was unsure about bigger registers in real mode. My memories were telling me you can use extensions in real mode. I googled it and it seems my memories were not lying to me -

from: https://www.quora.com/When-an-x86-CPU-is-running-in-real-mode-can-it-be-considered-to-be-basically-an-8086-CPU-or-maybe-8088

“it still has all the fancy 32bit, MMX, SSE, etc, etc instructions, that work in real mode, ”

It is however important to note that the 64-bit integer registers are not available in 32-bit mode, so if you want to do 64-bit integer arithmetic you have to use MMX instructions, not the standard integer math instructions.

so newer processors keep the old registers ? i.e. a processor of current days still has 8 and 16 bit registers?

My project`s facebook page is “DreamLand Page”

Calin said:
so newer processors keep the old registers ? i.e. a processor of current days still has 8 and 16 bit registers?

A 10 minute Part 1 YouTube video on assembly would tell you this. RTFM, seriously. At least the first 10 pages.

I thought you were writing low-level system software in assembly just a month ago?

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

Calin said:

so newer processors keep the old registers ? i.e. a processor of current days still has 8 and 16 bit registers?

Yes. It is the same registers.
AL, AH, AX, EAX, RAX is all the very same register. Only the mode changes.
64 bit mode gives you 8 extra general registers and 8 extra SSE/AVX registers.

Download the manuals for the most updated and actual information - https://software.intel.com/en-us/articles/intel-sdm

Thanks NikiTo

My project`s facebook page is “DreamLand Page”

@Calin Notice - the variable's size depends of your needs. It can be less than the size of the register.

In one single register you can have various variables. This is something a compiler would not do for you.
If you need two integer counters and none of them never reaches values more than 255, you can have two of these counters in the same register at no additional cost. You can have 8 integer variables one byte each inside the same register at some small extra cost. With ASM sky is the limit.

(Assembler for AMD and Intel varies a bit. Running ASM code manually tuned for Intel could make the program crash on AMD CPUs. On AMD CPU if you write to the lower 32 bits of the 64bit register, the upper 32 bits remain unchanged. On Intel CPUs if you write to the lower half, the upper half resets to zero. This is the bad part of assembly language - the portability problem. But if compilers store an 8 bit variable inside 64 bits register, no problems! It is so much power completely gone wasted. But we sacrifice all that for productivity. I sacrifice it too. It is sad, but understandable.)

NikiTo said:
some small extra cost

What does extra cost mean? more CPU time to perform the math operations (with these variables) when compared with the normal situation when the variables are placed in a register one at a time?

My project`s facebook page is “DreamLand Page”

It takes more time to place and access the extra variable.
Math is the same.

mov ECX, variable1
mov EDX, variable2
;---------------------------------
mov EAX, ECX
bswap RAX ;use rol/ror if you want to move it to SSE/AVX regs later
or RAX, RDX

Now you have two DWORDS inside RAX.

You access the variables in reverse order. Two extra operations. But it is hella fast compared to having to move these variables to RAM. Access to non-cached RAM could cost as much as 700 cycles of the CPU!!!!!!!!

This topic is closed to new replies.

Advertisement