Assembler Programming Basics

Moving Data and Basic Arithmetic

Moving data into registers can be either from a memory location, another register or from a constant. Data can moved from a register to another register or a memory location. For example to move a constant value into the register eax the following instruction could be used:

mov eax,1

For example to move the contents of the register eax to a memory location the following instruction could be used:

mov 0[edi],eax or mov LocationA,eax

The data flow is right to left, but depends upon the assembler/processor. For the intel processors, eax is the destination and the 1 is the source value in the first example.

In the second example the memory area identified by are address in the register edi or the named memory location LocationA is the destination and the contents of the eax register is the source. The [] users the value within the square barckets as a memory address. For example

Register Indexing

In the example of 0[edi] as a destination address the contents of the edi register is used as a base memory location and the offset, in this case 0, is added to that memory address to obtain the final destination address.

Constant Padding and Truncating

Note: Constant values may be expanded to the destination length by appending zeros to the higher order bits. So a decimal value of 1 is converted and expanded into 00000000 00000000 00000000 00000001 which can be expressed in hexadecimal as 00000001. Or it can be truncated by dropping bits, which will normally generate a warning from the assembler. Constants can be written in assembler memonics using other bases than decimal (base 10) by using either a prefix or suffix to identifiy the base of the number. The prefix or suffix will depend upon the assembler program used.

For example

mov eax,5456534Fh

In this case the suffix is the 'h' to indicate a hexadecimal value (base 16)

Negative numbers are stored using a twos complement format, so -1 is expressed as 0FFFFFFFFh. To calculate the twos complement format, do the following:

Note: the inversion of the bit is called a NOT operation

The way the bits are stored in the actual processor is either most significant bit in the left most bit position (big endian) or in the right most bit position (little endian).so the instruction mov eax,5456534Fh would look like:

Note the processor determines the storage order, Intel compatible processors store using little endian. For example the instructions

mov eax,12345678h

mov [1024h],eax

The contents of the memory location [1024h – 1027h] would be 78h, 56h, 34h, 12h

Basic Arithmetic

Most modern processors provide two or three types of arithmetic operations. Each of these is based upon the numeric data type that they support.

The structure of packed decimal and floating point will be covered later

Originally integers and packed decimal where supported in hardware and floating point operations were performed in software, but most processors now support all three in hardware.

Each of these numeric data types has four standard mathematical operations that are supported by the processor. These are

Mathematical examples

Addition

mov eax,10

mov ebx,12

add eax,ebx

These instructions leaves the result of 22 or 16h or 0000 0000 0000 0000 0000 0000 0001 0110b in register eax and the value of ebx is unchanged.

Subtraction

mov eax,10

mov ebx,12

sub eax,ebx

These instructions leaves the result of -2 or ffff fffeh in register eax and the value of ebx is unchanged. Remember that negative numbers are stored in two complement form.

Multiplication

mov eax,10

mov ebx,12

imul ebx

These instructions leaves the result of 120 or 78h in register eax and 0h in edx. The value of ebx is unchanged. The multiply instruction has to cater for significant increases in size of the result, so it multiplies two 32 bit numbers and saves a 64 bit number in the register pair eax,edx. Also the eax register is hard coded into the instruction logic and is not specified in the instruction mnemonic.

Division

move dx,0

mov eax,12

mov ebx,6

idiv ebx

These instructions leaves the quotient result of 2 or 2h in register eax and the remainder result of 0h in edx. The value of ebx is unchanged. The divide instruction is the reverse of the multiply instruction, it divides a 64 bit in the register pair eax,edx by a 32 bit number and returns a 32 quotient and remainder.

The instruction mul is the unsigned version of imul and div is the unsigned version of idiv.


Back - Introduction
Next - Chapter 2 More Programming Basics