Computer Organization and Assembly Language (Notes) : Prepared by Umer Naeem (Umer - Naeem@ucp - Edu.pk) Arif Mustafa
Computer Organization and Assembly Language (Notes) : Prepared by Umer Naeem (Umer - Naeem@ucp - Edu.pk) Arif Mustafa
Prepared by
Umer Naeem ([email protected])
Arif Mustafa ([email protected])
Types of Registers
The registers are grouped into three categories:-
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.
Area highlighted with yellow is showing the ascii values of the contents displayed
in the memory m2.
Viewing sample variable in memory.
num1: dw 65
num2: dw 10
num3: dw 15
num4: dw 0
A sample program to read double word variable
[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.
JL Jump if less
Example 2:
Rotate right 3 times the value in register bx.
Let BX=0xEFCD
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
… … … …
… … … …
… … … …
… … … …
… …. … …
… … … …
; 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.
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)
String Examples
Simple String(Example) movsb(Example)
Using loop instruction(Example) Using REP instruction(Example)
Using SCAS instruction(Example)