0Day Forums
Why do we bother with CPU registers in assembly, instead of just working directly with memory? - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Assembly (https://0day.red/Forum-Assembly)
+--- Thread: Why do we bother with CPU registers in assembly, instead of just working directly with memory? (/Thread-Why-do-we-bother-with-CPU-registers-in-assembly-instead-of-just-working-directly-with-memory)



Why do we bother with CPU registers in assembly, instead of just working directly with memory? - rephael423067 - 07-24-2023

I have a basic question about assembly.

Why do we bother doing arithmetic operations only on registers if they can work on memory as well?

For example both of the following cause (essentially) the same value to be calculated as an answer:

Snippet 1

.data
var dd 00000400h

.code

Start:
add var,0000000Bh
mov eax,var
;breakpoint: var = 00000B04
End Start
<br>

Snippet 2

.code

Start:
mov eax,00000400h
add eax,0000000bh
;breakpoint: eax = 0000040B
End Start
<br><br>
From what I can see most texts and tutorials do arithmetic operations mostly on registers. Is it just faster to work with registers?


RE: Why do we bother with CPU registers in assembly, instead of just working directly with memory? - psi106 - 07-24-2023

Registers are accessed **way** faster than RAM memory, since you don't have to access the "slow" memory bus!


RE: Why do we bother with CPU registers in assembly, instead of just working directly with memory? - premiate971939 - 07-24-2023

Yes, it's much much much faster to use registers. Even if you only consider the physical distance from processor to register compared to proc to memory, you save a lot of time by not sending electrons so far, and that means you can run at a higher clock rate.


RE: Why do we bother with CPU registers in assembly, instead of just working directly with memory? - roley938 - 07-24-2023

Yes - also you can typically push/pop registers easily for calling procedures, handling interrupts, etc


RE: Why do we bother with CPU registers in assembly, instead of just working directly with memory? - moviemaking712544 - 07-24-2023

Registers are much faster and also the operations that you can perform directly on memory are far more limited.


RE: Why do we bother with CPU registers in assembly, instead of just working directly with memory? - unimpassionedness29839 - 07-24-2023

It's just that the instruction set will not allow you to do such complex operations:

add [0x40001234],[0x40002234]

You have to go through the registers.