0% found this document useful (0 votes)
5 views

Data Transfers, Addressing, and Arithmetic

The document provides an overview of data transfer, addressing, and arithmetic instructions in assembly language, detailing how data is moved between registers and memory. It explains various addressing modes, including immediate, register, and memory addressing, as well as arithmetic operations such as addition, multiplication, and division. Additionally, it covers stack operations, the use of the compare instruction, and conditional jump and loop instructions for control flow in programming.

Uploaded by

Eman Mansoor
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Data Transfers, Addressing, and Arithmetic

The document provides an overview of data transfer, addressing, and arithmetic instructions in assembly language, detailing how data is moved between registers and memory. It explains various addressing modes, including immediate, register, and memory addressing, as well as arithmetic operations such as addition, multiplication, and division. Additionally, it covers stack operations, the use of the compare instruction, and conditional jump and loop instructions for control flow in programming.

Uploaded by

Eman Mansoor
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

DATA TRANSFERS,

ADDRESSING, AND Dr. Muhammad


Haroon
ARITHMETIC
CHAPTER OVERVIEW
1. Data Transfer Instructions
2. Addressing Instructions
3. Arithmetic Instructions
4. Jump and Loop instructions
MEMORY RECALL
EAX is the full 32-bit value

AX is the lower 16-bits

AH is the bits 8 through 15 (zero-based), the top half


of AX

AL is the lower 8 bits


1.1- DATA TRANSFERS
Data transfer instructions are used to move data between registers,
and memory.
Register-to-Register Transfer
MOV AX, BX;
Move the value in register BX into register AX, The original value of
BX remains unchanged.
Memory-to-Register Transfer
MOV AX, [1234H] ;
The instruction accesses memory location 1234H and loads the data
into register AX. Memory content remains unchanged.
Register-to-Memory Transfer
MOV [1234H], AX ;
The value in AX is copied to the memory location 1234H. AX is
unchanged.
TRANSFER OF IMMEDIATE
VALUE
Immediate value
An immediate value could be represented in
assembly as a numeric literal like 123.
Immediate-to-Register Transfer
MOV AX, 5 ;
The constant value 5 is directly loaded into AX.
1.2- STACK OPERATIONS
the stack is a structure where we put values and the
last object will put in will be the first one we get out
(LIFO architecture).

The stack is an area in memory that is maintained by


two special registers, esp and ebp. Respectively,
they are called the stack pointer and the base
pointer.
STACK POINTER
The purpose of esp is to point at the end of the stack.
(push & pop)
In most architectures the stack grows downward.
So when we push a value on the stack (like an int)
we would put it at the address esp points to and
move esp.
PUSH AX; Save the value of AX on the stack
STACK POINTER
POP
We we get values out of the stack (called popping values) we do
the opposite, so the code:
POP AX; removes the top value from the stack and places it in AX

The general reason for using register pushes and pops is to be able
to temporarily store a register value, use the register for something
else, then get that value back.
BASE POINTER
The stack pointer points to the top item on the stack
and the base pointer points to the "previous" top of
the stack before the function was called.
It helps access data on the stack relative to a fixed
location, making code easier to read.
Unlike the stack pointer (ESP), which moves during
push/pop operations, the base pointer usually
remains constant throughout the execution of a
function.
The base pointer remains a vital concept for
debugging.
2.0 ADDRESSING
Addressing modes define how the processor
identifies the location of data (operands).
Operand Types
Immediate – a constant integer (8, 16, or 32 bits).
Value is encoded within the instruction
Register – the name of a register. Register name is
converted to a number and encoded within the
instruction
Memory – reference to a location in memory.
Memory address is encoded within the instruction, or
a register holds the address of a memory location
2.1-IMMEDIATE ADDRESSING
Immediate Addressing is an addressing mode in
assembly language where the operand (data) is
directly specified in the instruction itself.
This means the value to be operated on is included
as part of the instruction, rather than being stored in
a register or memory location.
Immediate addressing is fast since the operand is
already part of the instruction, avoiding memory
access.
IMMEDIATE ADDRESSING
In x86 Assembly: Immediate values are prefixed by $
or appear without one.
mov eax, 10; Move the immediate value 10 into the
EAX register add eax, 5 ; Add the immediate value
5 to EAX
Use Cases
Initializing registers with constants.
Performing calculations with fixed values.
Setting flags based on specific constants.
2.2-REGISTER ADDRESSING
Register Addressing is an addressing mode in
assembly language where the operand is stored in a
register rather than in memory.
This mode is highly efficient because registers are
the fastest storage locations in a processor,
providing quick access to data.
In x86 assembly,
mov eax, ebx; Copy the value in EBX into EAX
add eax, ecx; Add the value in ECX to EAX
EXAMPLES OF REGISTER
ADDRESSING
Data Movement: In Register Addressing, data movement
refers to the transfer of data between registers within a
CPU.
mov eax, ebx; Copy the value in EBX into EAX
MOV R1, 10; Load immediate value 10 into register
R1
ADD R3, R1, R2; Add the values in R1 and R2, store
result in R3
data is moved between registers efficiently for
computation. Register addressing is a fundamental
addressing mode for processors, central to their high-
performance capabilities.
EXAMPLES OF REGISTER
ADDRESSING
Arithmetic Operations: Arithmetic operations in the
context of computer systems refer to basic
mathematical operations that can be performed on
data.
add eax, ecx; Add the value in ECX to EAX
sub edx, eax; Subtract EAX from EDX
EXAMPLES OF REGISTER
ADDRESSING
Arithmetic Operations: Arithmetic operations in the
context of computer systems refer to basic
mathematical operations that can be performed on
data.
add eax, ecx; Add the value in ECX to EAX
sub edx, eax; Subtract EAX from EDX

In modern computers, arithmetic operations are often


carried out using ALUs, which are dedicated hardware
units within the CPU designed to handle these
operations efficiently.
2.2-EXAMPLES OF REGISTER
ADDRESSING
Logical Operations: Logical operations in assembly
language are used to manipulate data at the bit
level.
and eax, ebx; Perform bitwise AND between EAX
and EBX
MOV R1, 11110000; R1 = 11110000
MOV R2, 00001111; R2 = 00001111
 AND R1, R2 ; R1 = 00000000 (result of
bitwise AND)
 OR R1, R2 ; R1 = 11111111 (result of
bitwise OR)
EXAMPLES OF REGISTER
ADDRESSING
Bit Shifting: Bit shifting operations are commonly
used in a variety of low-level programming tasks,
providing a highly efficient way to manipulate
individual bits or groups of bits within a value.
They are particularly useful when working with
hardware control, data processing, and
mathematical operations.
Lets discuss some key use cases of bit shifting in
assembly and systems programming.
EXAMPLES OF REGISTER
ADDRESSING
Example of Hardware Control
Bit shifting allows you to move bits into specific positions,
making it easier to read, set, or clear individual bits in the
registers.
Clearing Specific Flags Imagine a device control register
where each bit represents a different flag:
Bit 0: Enable/Disable device
Bit 1: Start/Stop operation
Bit 2: Interrupt Enable
Bit 3: Power Mode
EXAMPLES OF REGISTER
ADDRESSING
Example of Mathematical Operations
Multiplying by 2: A left shift by 1 position (SHL)
effectively multiplies a number by 2.
MOV R1, 8; R1 = 8
SHL R1, #1; R1 = 16 (multiplication by 2)
Dividing by 2: A right shift by 1 position (SHR) divides a
number by 2.
MOV R2, 16; R2 = 16SHR
R2, #1; R2 = 8 (division by 2)
2.3-MEMORY ADDRESSING
Memory Addressing refers to the method of specifying
the location of operands in memory (as opposed to
registers) in assembly language.
When using memory addressing, the operand is
stored in a specific memory location, and the
instruction uses an address to access it.
There are several types of memory addressing modes
in assembly, each with its own way of specifying
where the operand is located in memory.
Let’s explore the main types:
2.3-TYPES OF MEMORY
ADDRESSING MODES
Direct Addressing: Where the operand's memory address
is explicitly specified in the instruction. Indirect
addressing useful in many scenarios, such as working
with arrays, or linked lists.
mov eax, [0x1234] ; Move the value at memory
address 0x1234 into EAX
In this case, the operand is at memory address 0x1234.
2.3-TYPES OF MEMORY
ADDRESSING MODES
Indirect Addressing: The instruction uses a register or a
memory location to store the address of the operand.
Example (using a register)
mov eax, [ebx]; Move the value at the address in EBX
into EAX.
The value stored in EBX is used as a pointer to the
memory address of the operand.
TYPES OF MEMORY
ADDRESSING MODES
Indexed Addressing: it allows you to access a specific
element in an array or a list by adding an index
(position) to a base address (the start of the array).
The base address is where the array starts.
The index tells which element in the array to access.
The effective address is calculated by adding the base
address and the index.
INDEXED ADDRESSING:
Example:
Imagine you have an array of 4 integers.
Array = [10, 20, 30, 40] and Base Address = 0x1000
(starting address of the array)
If you want to access the third element (30) in the array
using indexed addressing, you'd add the index (2) (for the
3rd element) to the base address (0x1000).
Index: 2 (because we want the 3rd element, and indexing
starts from 0)
Element size: 4 bytes (each integer is 4 bytes)
INDEXED ADDRESSING:
So, the effective address where the 3rd element (30) is
stored will be:
Effective Address = Base Address + (Index * Element
Size)
Effective Address = 0x1000 + (2 * 4)
Effective Address = 0x1000 + 8 = 0x1008
Now, the processor will look at memory address 0x1008,
and retrieve the value stored there, which is 30
3. ARITHMETIC
Arithmetic instructions perform calculations like
addition, subtraction, multiplication, division, Bitwise
Arithmetic and Increment and Decrement.
Multiplication
MOV AX, 5
MOV BX, 3
MUL BX; AX = AX * BX
MUL BX multiplies the values in AX and BX. For
multiplication, the result is stored in AX.
3.1-MULTIPLICATION (THE MUL/IMUL INSTRUCTION)

The MUL (Multiply) instruction handles unsigned data and


the IMUL (Integer Multiply) handles signed data.
Both instructions affect the Carry and Overflow flag.
Multiplicand in both cases will be in an accumulator,
depending upon the size of the multiplicand and the
multiplier and the generated product is also stored in two
registers depending upon the size of the operands.
Following section explains MUL instructions with three
different cases.
WHEN TWO BYTES ARE
MULTIPLIED
The multiplicand is in the AL register, and the
multiplier is in the memory or in another register.
The product is in AX. High-order 8 bits of the
product is stored in AH and the low-order 8 bits
are stored in AL.
WHEN TWO ONE-WORD VALUES ARE MULTIPLIED
The multiplicand should be in the AX register, and the
multiplier is in memory or another register.
For example, for an instruction like MUL DX, you must
store the multiplier in DX and the multiplicand in AX.
The resultant product is a doubleword, which will need
two registers. The high-order (leftmost) portion gets
stored in DX and the lower-order (rightmost) portion gets
stored in AX.
WHEN TWO DOUBLEWORD VALUES ARE MULTIPLIED
When two doubleword values are multiplied, the
multiplicand should be in EAX and the multiplier is a
doubleword value stored in memory or in another register.
The product generated is stored in the EDX:EAX registers,
i.e., the high order 32 bits gets stored in the EDX register
and the low order 32-bits are stored in the EAX register.
3.2- THE DIV/IDIV
INSTRUCTIONS
The division operation generates two elements –
a quotient and a remainder.
In case of multiplication, overflow does not occur
because double-length registers are used to keep the
product.
However, in case of division, overflow may occur.
The processor generates an interrupt if overflow occurs.
75/9
3.2- THE DIV/IDIV
INSTRUCTIONS
The dividend is in an accumulator.
Both the instructions can work with 8-bit, 16-bit or
32-bit operands.
The operation affects all six status flags.
Following section explains three cases of division
with different operand size
WHEN THE DIVISOR IS 1 BYTE
The dividend is assumed to be in the AX register (16 bits).
After division, the quotient goes to the AL register and the
remainder goes to the AH register.
WHEN THE DIVISOR IS 1 WORD
The dividend is assumed to be 32 bits long and in the
DX:AX registers. The high-order 16 bits are in DX and the
low-order 16 bits are in AX. After division, the 16-bit
quotient goes to the AX register and the 16-bit remainder
goes to the DX register.
3.2- WHEN THE DIVISOR IS
DOUBLEWORD
The dividend is assumed to be 64 bits long and in the
EDX:EAX registers.
The high-order 32 bits are in EDX and the low-order 32
bits are in EAX. After division, the 32-bit quotient goes
to the EAX register and the 32-bit remainder goes to
the EDX register.
3.3 INCREMENT AND
DECREMENT IN X86
The inc operation increases the value of a register by 1.
The dec operation decreases the value of a register by 1.
The INC (increment) and DEC (decrement) instructions in
x86 assembly primarily operate on registers or memory
locations, but not directly on constants (immediate
values).
Operations like INC and DEC are intended to modify
stored values, such as those in registers or memory,
where the results are meaningful and can be reused.
3.3-INCREMENT/DECREMENT
EXAMPLE
Registers
INC eax ; Increment the value in the EAX register
DEC ecx ; Decrement the value in the ECX register
Memory Locations
INC [1234] ; Increment the value at the memory
location `1234`
DEC [1234] ; Decrement the value at the memory
location `1234`
3.3-INCREMENT/DECREMENT EXAMPLE
But you cannot use them directly on constants:
INC 5 ; Invalid
DEC 100 ; Invalid
Alternative for Constants
mov ax, 10; move 10 into the AX register. In
hexadecimal, this is A.
inc ax value increases to B (or 11 in decimal).
This is how we can increase the value of a register by 1.
4.0-COMPARE (CMP)
INSTRUCTION
The CMP instruction compares two operands. It is
generally used in conditional execution.
This instruction basically subtracts one operand from the
other for comparing whether the operands are equal or
not.
It does not disturb the destination or source operands.
It is used along with the conditional jump instruction for
decision making.
SYNTAX
CMP destination, source
CMP compares two numeric data fields.
The destination operand could be either in register
or in memory.
The source operand could be a constant
(immediate) data, register or memory.
4.0- EXAMPLE - COMPARE
INSTRUCTION
CMP DX, 00; Compare the DX value with zero
If yes/no than Condition/decision
CMP is often used for comparing whether a counter value
has reached the number of times a loop needs to be run.
INC EDX
CMP EDX, 10; Compares whether the counter has
reached 10
JLE LP1 ; If it is less than or equal to 10, then
jump to LP1
4.1 JUMP AND LOOP
INSTRUCTIONS
Conditional execution in assembly language is
accomplished by several looping instructions.
These instructions can change the flow of control in a
program. Conditional execution is observed in two
scenarios.
Conditional jump
Unconditional jump
4.1- CONDITIONAL JUMP
This is performed by a set of jump instructions
j<condition> depending upon the condition.
The conditional instructions transfer the control by
breaking the sequential flow and they do it by changing
the offset value in Instruction Pointer.
If some specified condition is satisfied in conditional jump,
the control flow is transferred to a target instruction.
There are numerous conditional jump instructions
depending upon the condition and data.
CONDITIONAL JUMP
INSTRUCTIONS
CONDITIONAL JUMP INSTRUCTIONS
The following conditional jump instructions have special
uses and check the value of flags
4.2- UNCONDITIONAL JUMP
Transfers control to a specified address unconditionally.
Transfer of control may be forward, to execute a new set
of instructions or backward, to re-execute the same steps.
Syntax
The JMP instruction provides a label name where the flow
of control is transferred immediately. The syntax of the
JMP instruction is.
JMP label
4.3 LOOP INSTRUCTIONS
The JMP instruction can be used for
implementing loops.
In LOOP instruction, ECX register contains the
loop count.
When the loop instruction is executed, the ECX
register is decremented and the control jumps to
the target label, until the ECX register value, i.e.,
the counter reaches the value zero.
4.3 LOOP INSTRUCTIONS
Syntax:
LOOP label
For example, the following code can be used for
executing the loop-body 10 times.
MOV CL, 10
L1:
<LOOP-BODY>
DEC CL
JNZ L1
Thankyou

You might also like