Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly and Registers

Tags:

x86

assembly

I am learning assembly language in my spare time to become a better developer. I have bought a book from Amazon and it will be delivered soon and I am trying to familiarise myself with the basic concepts before I dig in.

I am confused about which general purpose registers I should use for general purpose tasks. I know the C programming language and I have read about calling conventions for assembly and the Windows API.

However, if you have a very simple task e.g. add two numbers together with no references to C etc, then which registers would you use?

I realise this is a basic question.

like image 627
w0051977 Avatar asked Jul 30 '26 07:07

w0051977


1 Answers

Whichever ones you want, really.

In first-generation 8086 processors, some of the general-purpose registers (AX, BX, CX, DX) had semi-special purposes as well. For example, the LOOP opcode assumes that your loop counter is in CX; and the DIV opcode stores the results in AX and DX. And some addressing modes could use BX or SI or DI as an offset, but couldn't use the other general-purpose registers as offsets.

Over succeeding processor generations, some of those special purposes have faded -- for example, I think most opcodes now let you use any register as an offset, not just BX. So for the most part, the general-purpose registers are interchangeable.

If you want to follow historical precedent, (E)AX is the "accumulator", so it would be the traditional place to store e.g. the running total as you're summing a series of numbers. So if you're doing addition, and you're not sure which register to put the result in, (E)AX would be a good default choice.

like image 67
Joe White Avatar answered Aug 02 '26 08:08

Joe White



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!