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

Computer Organization and Assembly Language (Notes) : Prepared by Umer Naeem (Umer - Naeem@ucp - Edu.pk) Arif Mustafa

This document discusses computer organization and assembly language concepts such as registers, variables, memory, addressing modes, jump instructions, and stack operations. It includes examples of adding variables using different addressing modes and conditional jumps. Key register types are general purpose, control, and segment registers. Variables can be bytes, words, or double words. Memory can be viewed and variables accessed using direct, register indirect, indexed, and based addressing modes. The stack is used for subroutine calls and returns using PUSH, POP, CALL, and RET instructions.

Uploaded by

Khizar Iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
352 views

Computer Organization and Assembly Language (Notes) : Prepared by Umer Naeem (Umer - Naeem@ucp - Edu.pk) Arif Mustafa

This document discusses computer organization and assembly language concepts such as registers, variables, memory, addressing modes, jump instructions, and stack operations. It includes examples of adding variables using different addressing modes and conditional jumps. Key register types are general purpose, control, and segment registers. Variables can be bytes, words, or double words. Memory can be viewed and variables accessed using direct, register indirect, indexed, and based addressing modes. The stack is used for subroutine calls and returns using PUSH, POP, CALL, and RET instructions.

Uploaded by

Khizar Iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Computer Organization and

Assembly Language (Notes)

Prepared by
Umer Naeem ([email protected])
Arif Mustafa ([email protected])
Types of Registers
The registers are grouped into three categories:-

1. General Purpose registers


1.1. Data registers
1.1.1. AX is the primary accumulator.
1.1.2. BX is known as the base register.
1.1.3. CX is known as the count register.
1.1.4. DX is known as the data register.
1.2. Pointer registers
1.2.1. Instruction Pointer IP
1.2.2. Stack Pointer SP
1.2.3. Base Pointer BP
1.3. Index registers
1.3.1. Source Index SI
1.3.2. Destination Index DI
2. Control registers
2.1. Instruction Pointer and Flag register
3. Segment registers
3.1. Code Segment CS
3.2. Data Segment DS
3.3. Stack Segment SS
3.4. Extra Segment ES
Types of variables
Type No. of bits Example declaration:

Byte 8 Num1: db 43
Word=> 2 bytes 16 Num2: dw 0xABFF
double word=> 2 words 32 Num3: dd 0xABCDEF56

Note: size of both operands must be same for any type of instruction.

For example:
Mov ax,dh ;is wrong because destination is 2 bytes and source is 1 byte.

Viewing memory in DOSBOX


Areas highlighted in red( memory 1) “m1” and blue (memory 2) “m2” are showing
the memory contents. Note: Two copies of the same memory is displayed in the
given windows.

Area highlighted with yellow is showing the ascii values of the contents displayed
in the memory m2.
Viewing sample variable in memory.

➢ To view memory from window m2 run the command “m2 ds:Addressofvariable”


example: m2 ds:011F
➢ A variable with name “num1” is initialized at memory location 11F with value 65
decimal.
41 hex = 65 decimal is the ascii of “A”.

A sample program to add three numbers using memory variables


[org 0x0100]
mov ax, [num1] ; load first number in ax
mov bx, [num2] ; load second number in bx
add ax, bx ; accumulate sum in ax
mov bx, [num3] ; load third number in bx
add ax, bx ; accumulate sum in ax
mov [num4], ax ; store sum in num4

mov ax, 0x4c00 ; terminate program


int 0x21

num1: dw 65
num2: dw 10
num3: dw 15
num4: dw 0
A sample program to read double word variable

mov bx,[num5];load lower 2 bytes of num5 in bx register.


mov cx,[num5+2];load higher 2 bytes of num5 in cx register.
num5: dd 0x12345678
mov ax, 0x4c00 ; terminate program
int 0x21

Types of Addressing Modes


Direct
A fixed offset is given in brackets and the memory at that offset is
accessed. For example “mov [1234], ax” stores the contents of the • Mov ax,[num1]
;reading
AX registers in two bytes starting at address 1234 in the current data • Mov [num2],ax ;writing
segment. The instruction “mov [1234], al” stores the contents of the
AL register in the byte at offset 1234.
Based Register Indirect
A base register is used in brackets and the actual address accessed
depends on the value contained in that register. For example “mov • Mov bx,var
[bx], ax” moves the two byte contents of the AX register to the
address contained in the BX register in the current data segment. The • Mov cx,[bx]
instruction “mov [bp], al” moves the one byte content of the AL • Mov [bx],ax
register to the address contained in the BP register in the current stack
segment.
Indexed Register Indirect
An index register is used in brackets and the actual address accessed • Mov si,var1
depends on the value contained in that register. For example “mov • Mov di,var2
[si], ax” moves the contents of the AX register to the word starting at
• Mov [si], ax
address contained in SI in the current data segment. The instruction • Mov [di],bx
“mov [di], ax” moves the word contained in AX to the offset stored • Mov cx,[si]
in DI in the current data • Mov dx,[di]
segment.
Based Register Indirect + Offset
A base register is used with a constant offset in this addressing mode.
The value contained in the base register is added with the constant
offset to get the effective address. For example “mov [bx+300], ax”
stores the word contained in AX at the offset attained by adding 300 • mov [bx+3], ax
to BX in the current data segment. The instruction “mov [bp+300], • mov cl,[bp+5]
ax” stores the word in AX to the offset attained by adding 300 to BP
in the current stack segment.

A program to add three variables using Direct addressing

A program to add three variables using InDirect addressing


A program to add three numbers using only one location.

When using variables of different size

[org 0x100]
mov ax,0
mov bx,0
mov cx,0
mov dx,0
; add two variables their sum should be 5163 or 142B
;option1
Mov al,[var1]
Mov bl,[var2]
Add al,bl
;option2
Mov al,[var1]
Mov bx,[var2]
Add al,bx ;this will show error because of size mismatch change al to ax then run
again
;option3
Mov ax,[var1] ;why ax is not showing the correct value of var1
Mov bx,[var2]
Add ax,bx
;option4
Mov al,[var1]
Mov ah,0 ;already 0 in ah
Mov bx,[var2]
Add ax,bx
mov ax,0x4c00
int 21h
var1: db 60 ;0x3C
var2: dw 5103 ;0x13EF
JUMP INSTRUCTIONS
Two main types of jump instructions

a) Unconditional jump:

b) Conditional jump:
To start executing instructions based on some condition.

Some conditional jumps are as follows:

JC Jump if carry flag set


JNC Jump if not carry
JZ Jump if zero flag set
JE Jump if zero flag set
JNZ Jump if zero flag not set
JNE Jump if zero flag not set
JS Jump if sign flag is set
JNS Jump if sign flag not set
JP Jump if parity flag is set
JNP Jump if not parity
JO Jump if overflow
JNO Jump if not overflow

1. Conditional jumps after signed operand comparison


JG Jump if greater

JNG Jump if not greater

JGE Jump if greater or equal

JNGE Jump if not greater or equal

JL Jump if less

JNL jump if not less

JLE Jump if less or equal

JNLE jump if not less or equal

2. Conditional jumps after unsigned operand


comparison
JA Jump if above
JNA Jump if not above
JAE Jump if above or equal
JNAE Jump if not above or equal
JB Jump if below
JNB Jump if not below
JBE Jump if below or equal
JNBE jump if not below or equal
• Compare Instruction
cmp operand1,operand2
It subtracts operand2 from operand1 and updates the flags only
without updating the value of the operands.

Example: Signed number comparison

In this example it compares two numbers and stores 1 in ax if al is


greater than bl else 0.
Example: Unsigned number comparison

TO ACCESS NEXT ELEMENTS WITHIN AN ARRAY ADD


OFFSETS TO ARRAY NAME DEPENDING UPON ARRAY SIZE
TYPE.
Access data from an array (Indexed register Indirect mode)

Shifting and Rotations variations


Example 1:
Multiply the number by 4 using shift operator.
Let the number is 5.

Example 2:
Rotate right 3 times the value in register bx.
Let BX=0xEFCD

Extended addition and subtraction


Example 3(Extended addition)
Example 4(Extended subtraction)

Stack operations
Observe the values of SP, IP in each code after push, pop, call
and ret instructions carefully.
PUSH
PUSH decrements SP (the stack pointer) by two and then transfers a word from the source operand
to the top of stack now pointed to by SP. PUSH often is used to place parameters on the stack
before calling a procedure; more generally, it is the basic means of storing temporary data on the
stack. For example “push ax” will push the current value of the AX register on the stack. The
operation of PUSH is shown below.

POP
POP transfers the word at the current top of stack (pointed to by SP) to the destination
operand and then increments SP by two to point to the new top of stack. POP can be used to move
temporary variables from the stack to registers or memory. Observe that the operand of PUSH is
called a source operand since the data is moving to the stack from the operand, while the operand
of POP is called destination since data is moving from the stack to the operand. The operation of
“pop ax” is shown below.

CALL
CALL activates an out-of-line procedure, saving information on the stack to permit a RET
(return) instruction in the procedure to transfer control back to the instruction following the CALL.
For an intra segment direct CALL, SP is decremented by two and IP is pushed onto the stack. The
target procedure’s relative displacement from the CALL instruction is then added to the instruction
pointer.

RET
RET (Return) transfers control from a procedure back to the instruction following the CALL that
activated the procedure. RET pops the word at the top of the stack (pointed to by register SP)
into the instruction pointer and increments SP by two. If RET is used the word at the top of the
stack is popped into the IP register and SP is incremented by two. If an optional pop value has
been specified, RET adds that value to SP. This feature may be used to discard parameters
pushed onto the stack before the execution of the CALL instruction.

Stack Example:
ADD Two Numbers that are pushed in stack without POP
Implementation of subroutine
Subroutine Example 1:
USING BP (Base Pointer)
Subroutine Example 2:
WITHOUT USING BP (Base Pointer)
Simple Reading a character ascii example:
Subroutine Example 3:
VIDEO MEMORY
Console Display:
Note : Each cell represents a word (2 byte).
Row 1,Col 1 Row 1,Col 2 …. Row 1,Col 80

Row 2,Col 1 Row 2,Col 2 … Row 2,Col 80

… … … …

… … … …

… … … …

… … … …

… …. … …
… … … …

Row 25,Col 1 Row 25,Col 2 … … … Row 25,Col 80

; if you change the second byte, you can change the color of the character.
; character attribute is 8 bit value,
; high 4 bits set background color and low 4 bits set foreground color.

LET AX have 16 bits with character ‘A’ as a value byte and Brown background with white
foreground color.

Blinking of
the
Attribute byte Value byte
foreground
color Background Foreground

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

0 1 1 0 1 1 1 1 0 1 0 0 0 0 0 1
; hex bin color
;0 0000 black
;1 0001 blue
;2 0010 green
;3 0011 cyan possible background colors
;4 0100 red
;5 0101 magenta
;6 0110 brown
;7 0111 light gray possible foreground color
;8 1000 dark gray
;9 1001 light blue
;a 1010 light green
;b 1011 light cyan
;c 1100 light red
;d 1101 light magenta
;e 1110 yellow
;f 1111 white
Copy character array from one to another.

“To run code without debugging simply type test.com instead


of afd test.com”
Type cls then enter before running the following codes.
Display string on screen
For example:
● Different attribute values of each word
● Different locations can be accessed for the display.

Display code which writes and clears the string from screen.
USE CTRL+F11 to reduce cycles / sec or CTRL+F12 to increase the speed of dosbox.

Slow down the speed of dosbox by press and hold ctrl and press F11 till 1 cycle
ASCII CODES
HEX format
ASCII CODES
Decimal format
String Instructions
Instruction Functionality actually
performed
movsb 1. Mov [ES:DI],[DS:SI]
2. Inc si Invalid instruction
3. Inc di (memory to memory)

movsw 1. Mov [ES:DI],[DS:SI] Invalid instruction


2. Add si,2 (memory to memory)
3. Add di,2
scasb 1. Cmp al,[ES:DI];ZF=1 if same
2. Inc DI
scasw 1. Cmp ax,[DI];ZF=1 if same
2. Add DI,2
cmpsb 1. Cmp [DS:SI],[ES:DI];ZF=1 if same Invalid instruction
2. Inc SI (memory to memory)
3. Inc DI
cmpsw 1. Cmp [DS:SI],[ES:DI];ZF=1 if same Invalid instruction
2. Add si,2 (memory to memory)
3. Add di,2
lodsb 1. Mov al,[DS:SI]
2. Inc si
lodsw 1. Mov ax,[DS:SI]
2. Add si,2
stosb 1. Mov [ES:DI],al
2. Inc di
stosw 1. Mov [ES:DI],ax
2. Add di,2
Rep It repeats the instruction cx times.

Repe It executes the instruction cx times or until


zf remains 1.
Repne It executes the instruction cx times or exit
when zf becomes 1.
Note: All yellow highlighted instructions will depend upon direction flag(cld, std) see second
last example.

String Examples
Simple String(Example) movsb(Example)
Using loop instruction(Example) Using REP instruction(Example)
Using SCAS instruction(Example)

Using CMPS instruction(Example)


Using LODSB instruction(Example) Using STOSB instruction(Example)
Traversing array from left to right

Traversing array from right to left


Using string operations with video memory.
Software Interrupts
Example 1: Printing Character Using Interrupt

Example 2: Printing String Using Interrupt


Example 3: (Taking Input from User and display)

Example 4: (Take input from User until they press Esc)


Example 5: (Taking Input from User setting cursor position display the
character)
Interrupts hooking and unhooking
Interrupt vector table-address mapping
• Offset: n*4 ; offset address of nth interrupt
• Segment: n*4+2 ; base address of nth interrupt

If N is the interrupt number then following operations are


executed by the INT and IRET by the processor.
Interrupt zero: INT 0

Example 6: ISR(Interrupt Service Routine) hooking-interrupt


zero
Example 7: Another Interrupt hooking
Example 8: Interrupt hooking without using INT instruction

Example 9: Interrupt unhooking.

You might also like