Jump to content
xisto Community
Sign in to follow this  
HannahI

Assemby Programming Intel Syntax Chapter 1: Basic Math, Move, And The Registers

Recommended Posts

Welcome to my Assembler language tutorial! Of course, this is going to be one of the longest tutorial because its the first one! I hope you'll continue learning after this tutorial. First off, let me show you a few basic things in assembly that will be very useful in later parts of my guide. Comments and sections. A comment, for those who don't know, is just a piece of English (well, actually it can be any language :P ) that will help support your code. The computer skips over this code, its just for human interaction. Now sections on the other hand are a bit different. The only section I'm going to talk about right now is the text section. This is where all your real code will go. The basic syntax for starting a section is shown in Example 1.1. You can change the text part to any section, not just text. They're pretty basic to use. Comments are even easier, though. You simply put a semi-colon after some code and the rest of the line is ignored by the assembler. This is shown in Example 1.2.

 

Example 1.1

section .text
Example 1.2

some code;a comment

If you programmed in a higher a language before, you'll know what variables are. If you don't, you probbably remember them from algebra. They're just basicly a word that can store a value. But in assembly, they're called registers and have a slightly different purpose. If the purpose were the same, they would be called variables, not registers. Each register is assigned a name thats either 2 or 3 letters long. The reason why they're registers and not variables is that they have assigned names and purposes. In this part I'll talk about the four general purpose registers. These are registers that don't really have an assigned purpose, they're just for, well, general purpose. :o The four general purpose registers are ax, bx, cx, dx. The can be used for anything you want.

 

Now you may be wondering how to access these registers. The answers is simple, you use the move instruction. When writing assembly, you write mov instead of move. It's a little confusing at first, but then you'll get the hang of it. The syntax for the move instruction is shown in Example 1.3. When the assembler reads the code in Example 1.3, it says, "Ok memory, please put 8 at the location of the AX register". Using the move instruction shouldn't be too, too, complicated. Wrapping this partion of chapter 1 up, I'll show a basic real world example using move and the registers in Example 1.4.

 

Example 1.3

mov ax, 8
Example 1.4

section .text;start the programmov ax, 5;this is a random commentmov bx, 64mov cx, 4mov dx, 61

Now that I have wrapped up basic usage of registers, I will now show something more, well, useful. Arithmetic. Since it is that complex, I'll explain all four at once. Their names are add, sub, mul, and div. They do addition, subtraction, multiplication, and division. The work exactly like the move instruction, but instead of putting values into registers, they add, subtract, multiply, and divide values from register. You will use the same exact syntax as move, except where you would put how much you want to put in the register, you put how much you want to add, subtract, multiply, or divide, depending on the instruction you are using. Example 1.5 shows some basic usage of each of these.

 

Example 1.5:

mov ax, 3add ax, 2mov bx, axsub bx, 1mov cx, axadd cx, bxmul cx, 2mov dx, 6div dx, 2

Exercises

1. What is wrong in Exercise 1.1

Answer:

 

There is no comma on line 2

 

 

mov dx, 5add dx 3sub dx, 1mul dx, 2

2. Try to write a program the puts 7 in the CX register, multiplies by three, and clears the cx register without the move instruction (Think :P )

Answer:

 

 

mov cx, 7

mul cx, 3

sub cx, cx

 

 

 

3. How many general registers exist?

Answer

 

Four

 

 

Hope you learned something..and that you will want to continue learning stuff about assembly!

Share this post


Link to post
Share on other sites

 

Hello HannahI and thank you for the lessons how to learn about assembly coding.But i found some little bugs inside shown below.

Example 1.5:

....	   mul cx, 2	 ...	   div dx, 2
These both commands(mnemonics) above doesn´t exist. Existing commands are: "mul cx" and "div dx"!

An integer multiplication works in this example in combination with the ax-register and the register that we use with the mul-command.
So we multiplicate in this example the ax-register with the cx-register.

;--

An integer division works in this example in combination with the dx-register(highword) and the ax-register(low word) and will be divide by the register that we use with the div-command.
Hint: The dividend should not include the value of zero, because this will result a divide by zero error.
divisor = dx:ax
dividend = any existing register

Dirk

Share this post


Link to post
Share on other sites

divisor = dx:ax

dividend = any existing register

 

Dirk

Ups, sorry a littes mistake crapt in and i wrong sides out both terms (and i have no permission to edit my fault).

Here is the correct association:

 

dividend = dx:ax

divisor = any existing register

 

Hint: The divisor should not include the value of zero, because this will result a divide by zero error.

 

One example for an integer division:

mov dx, 0  ; high wordmov ax, 100; low wordmov bx, 5div bx
The result(20) will be stored into the ax-register.

 

Dirk

Edited by Dirk Wolfgang Glomp (see edit history)

Share this post


Link to post
Share on other sites

Oh, thanks for clarifying.

I like to help. :P
There are some more integer math instructions.

Increment:
inc ax
Decrement:
dec ax

Addition with carry (carry flag):
mov ax, 8001hmov bx, 8000hadd  ax, bxadc  dx, 1

Substaction with borrow (carry flag):
mov ax, 8000hmov bx, 8001hmov dx, 5sub  ax, bxsbb  dx, 1

Shift (bitwise) right (equivalent to an unsigned division by 2):
mov ax, 4; AX in bits = 00000100shr ax, 1; AX in bits = 00000010 = 2
Shr inserts a zero bit in the highest position(msb) and shifted out a bit on the lowest position(lsb).
(No flags are involved)


Shift (bitwise) left (equivalent to an unsigned multiplication by 2):
mov ax, 4; AX in bits = 00000100shl ax, 1; AX in bits = 00001000 = 8
Shl inserts a zero bit in the lowest position(lsb) and shifted out a bit on the highest position(msb).
(No flags are involved)


Load effective adress (only) for addition:
lea bx, [bx+4]; instead of "add bx, 4"
The primary purpose of the "lea" instruction is for to calculate the effective offset-address of a memory location, but we can use it also for an addition (possible with up to three operands).
Lea is one of the fastest instructions, so it will be alienated and used as a replacement for increment, addition, decrement, subtraction and for multiplication (with a scale factor of 2, 4 or 8 in combination with a 32Bit register).
(No flags are involved)

Dirk
Edited by Dirk Wolfgang Glomp (see edit history)

Share this post


Link to post
Share on other sites

By the way hannah, which assembly level software you use to execute these programs ? I mean i used to run assmebly codes on keil mp software and MPlab software. Which software you used to run these assembly level programs ? Nice tutorial there by by the way.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.