Page 1 of 1

Beginning x86 Assembly: Pointers, Addresses, Registers and the CPU.

#1 RCR  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 33
  • Joined: 04-July 12

Posted 08 July 2012 - 09:02 AM

On your journey through x86 Assembly, you will come across the term, "registers" constantly. Registers are the main part of Assembly, because they are one of the main components of the CPU. The CPU, or Central Processing Unit is one of the many hearts of a computer. The CPU has one huge job, to process everything. Yes, everything. In summary, my definition of a CPU would be the following:

"A CPU(also called a processor) is a component in a computer which reads instructions and process them through a language called assembly."

This is a fairly vague definition, but it explains the main job of the CPU, to process things. The registers in the CPU are the holding spaces for each instruction, before it is processed. If you're trying to learn Assembly, you probably know the basic concepts of memory, and how each piece of data has an address and a value. If you know C++/C, this will be a review for you, but for people just going from the ground up, you probably don't know this concept. A Pointer is simply saying, "to the value of". It points to the actual memory of whatever you're pointing to. I'll give you an example:

int pointer = 0x46;

int *point = pointer;


Whoa, what's that star? That's a pointer. In C++/C you define a pointer with an asterisk sign(*). Breaking it down, you can see that the variable point is pointing to the value of pointer which is 0x46(hexadecimal) which in decimal(base 10) 70.

Another concept you should know before you jump into assembly is addresses. I think you can sleuth out what it means, but I'll tell you anyway. It actually means, "at the address of". Here is an example:

int address = 0x46; //Same thing as before..but..

int addressOfThat = &address;


That was different, right? So we know that there is an int called address with the value of 0x46(70). Then there is an int called addressOfThat which is the address of address. So pretend that our address variable was located in memory at 10001001(137 in decimal), but it held the value 0x46, addressOfThat would hold the value 10001001 but could have a different address. Here's a little diagram I made to explain this:

Posted Image

Now for our last obstacle for this tutorial, registers. Before the CPU can convert to machine code and ship off our data and instructions, it needs to place it in groups for us to use while programming, these groups are called registers. If data is not put in registers before runtime, your assembly assembler(no, not compiler or interpreter) will crash and burn. This is because, when the assembler looks at your instructions, it will look for addresses and values, and if none of them are in the appropriate registers, it will think there is no data. This will put the assembler in a bewildered state, and it will spew data out all over the place. An example is for simple addition. Usually for arithmetic there is a register called ax(or eax in 32 bit mode) used. If I were to add two numbers, both would have to be in different registers. Here is an example of a quick assembly program to add 4 and 2.

MOV ax, 4
MOV bx, 2
ADD ax,bx



In the next tutorial, you will make your first program in assembly, but for now work on studying pointers, addresses, registers and the CPU. You will need them very soon :)

- RCR

Copyright © 2012-2016 RCR

This post has been edited by RCR: 08 July 2012 - 01:12 PM


Is This A Good Question/Topic? 2
  • +

Replies To: Beginning x86 Assembly: Pointers, Addresses, Registers and the CPU.

#2 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,300
  • Joined: 28-March 11

Posted 08 July 2012 - 09:12 AM

Wouldn't this be better suited in the tutorials section? Er, I'll move it.
Was This Post Helpful? 0
  • +
  • -

#3 stackoverflow  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 165
  • View blog
  • Posts: 545
  • Joined: 06-July 11

Posted 08 July 2012 - 12:31 PM

Thanks for sharing :)
Was This Post Helpful? 0
  • +
  • -

#4 RCR  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 33
  • Joined: 04-July 12

Posted 08 July 2012 - 12:58 PM

View Poststackoverflow, on 08 July 2012 - 12:31 PM, said:

Thanks for sharing :)


No problem, my pleasure. :)
Was This Post Helpful? 0
  • +
  • -

#5 ditikos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-July 12

Posted 09 July 2012 - 01:12 AM

Very nice tutorial.

I'd recommend the Art of Assembly though (http://flint.cs.yale.edu/cs422/doc/art-of-asm/pdf/) It's old but the perfect to start the x86 programming, remember it differentiates afterwards only on the memory registers (mainly).
Was This Post Helpful? 0
  • +
  • -

#6 dsenic464  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-February 09

Posted 09 July 2012 - 06:30 AM

Look at this segment:

int pointer = 0x46;

int *point = pointer;

Whoa, what's that star? That's a pointer.
In C++/C you define a pointer with an asterisk sign(*). Breaking it down, you can see that the

>>
variable point is pointing to the value of pointer which is 0x46(hexadecimal) which in decimal(base 10) 70.
<<

Variable POINT does NOT point to the variable POINTER; Variable POINT, in that case, HAS THE VALUE of the varialbe POINTER; If You wish the variable POINT to REALLY POINT TO the variable POINTER, You have to write:

int pointer = 0x46;
int *point = &pointer;

The sign '&' is the hearth of the story You talking about.
Next, the '*' is NOT the pointer by itself - it just DENOTES that variable POINT is POINTER.

Regards!
D.S.

Next thing I wish to warn You at, is the following:


int address = 0x46; //Same thing as before..but..
int addressOfThat = &address;

In some cases, You can get the compiler error (or, in best case, compiler's warning), which will tell You that You have to use TYPE CASTING, that is:

int address = 0x46;
int addressOfThat = (int)&address;

This error/warning could be issued due to the reason that the address of some variable is interpreted as UNSIGNED int.

Regards,
D.S.
Was This Post Helpful? 0
  • +
  • -

#7 opwernby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-May 12

Posted 09 July 2012 - 12:44 PM

View Postditikos, on 09 July 2012 - 01:12 AM, said:

Very nice tutorial.

I'd recommend the Art of Assembly though (http://flint.cs.yale.edu/cs422/doc/art-of-asm/pdf/) It's old but the perfect to start the x86 programming, remember it differentiates afterwards only on the memory registers (mainly).


This is a good thing to look at, also: it's a source code listing of the original BIOS on an AT. Why is this nice? Because it documents what all the "int" calls do in BIOS, e.g. int 10h = Display functions. and so on. Also has a link to the AT bios: http://www.rebelshav...c;f=52;t=000137

View Postopwernby, on 09 July 2012 - 12:41 PM, said:

View Postditikos, on 09 July 2012 - 01:12 AM, said:

Very nice tutorial.

I'd recommend the Art of Assembly though (http://flint.cs.yale.edu/cs422/doc/art-of-asm/pdf/) It's old but the perfect to start the x86 programming, remember it differentiates afterwards only on the memory registers (mainly).


This is a good thing to look at, also: it's a source code listing of the original BIOS on an AT. Why is this nice? Because it documents what all the "int" calls do in BIOS, e.g. int 10h = Display functions. and so on. Also has a link to the AT bios: http://www.rebelshav...c;f=52;t=000137


Sorry -- I meant, "It's a link to the source code for the original PC BIOS; also has a link to the AT BIOS." (more coffee required today!).
Was This Post Helpful? 0
  • +
  • -

#8 RCR  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 33
  • Joined: 04-July 12

Posted 14 July 2012 - 01:57 PM

View Postdsenic464, on 09 July 2012 - 06:30 AM, said:

Look at this segment:

int pointer = 0x46;

int *point = pointer;

Whoa, what's that star? That's a pointer.
In C++/C you define a pointer with an asterisk sign(*). Breaking it down, you can see that the

>>
variable point is pointing to the value of pointer which is 0x46(hexadecimal) which in decimal(base 10) 70.
<<

Variable POINT does NOT point to the variable POINTER; Variable POINT, in that case, HAS THE VALUE of the varialbe POINTER; If You wish the variable POINT to REALLY POINT TO the variable POINTER, You have to write:

int pointer = 0x46;
int *point = &pointer;

The sign '&' is the hearth of the story You talking about.
Next, the '*' is NOT the pointer by itself - it just DENOTES that variable POINT is POINTER.

Regards!
D.S.

Next thing I wish to warn You at, is the following:


int address = 0x46; //Same thing as before..but..
int addressOfThat = &address;

In some cases, You can get the compiler error (or, in best case, compiler's warning), which will tell You that You have to use TYPE CASTING, that is:

int address = 0x46;
int addressOfThat = (int)&address;
I
This error/warning could be issued due to the reason that the address of some variable is interpreted as UNSIGNED int.

Regards,
D.S.


Thank you for the warning about the type casting! But I do have some things to explain to you.
1) This was given as an example, so it is not meant to be real c++ or c, simply an example.
2) I did explain about pointers correctly, since one of the things I explained was that it means to the value of. This implies that point points to the value of that other variable.
3)Didn't I say that addresses point to the address of?

I hope you can realize that what I said was correct, and this was not a C++lesson in quote, "TYPE CASTING" or "POINTING". It was simply an introduction to assembly.

- RCR
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1