Professional Documents
Culture Documents
Lecture 05
Basic Instructions
ECEg - 4501 1
Basic Instructions AAiT
ECEg - 4501 3
Data Transfer Instructionscntd AAiT
The stack segment register and the stack pointer must be initialized
before these instructions can be used.
e.g.
PUSH BX Decrement SP by 2, copy BX to stack
PUSH DS Decrement SP by 2, copy DS to stack
POP DX Copy a word from top of stack to DX, Increment SP by 2
PUSH AL Illegal, must push a word
POP CS Just Illegal
PUSHF/POPF can be used with flags
ECEg - 4501 4
Data Transfer Instructionscntd AAiT
e.g.
LEA BX, PRICES Load BX with offset of PRICES in DS
LEA BP, SS: STACK_TOP Load BP with offset of STACK_TOP in SS
ECEg - 4501 6
2. Arithmetic Instructions AAiT
e.g.
ADD AL, 74H Add immediate number 74H to contents of AL.
Result In AL.
ADD DX,[SI] Add word from memory at offset [SI] in DS to
contents of DX. Result in DX.
ECEg - 4501 7
2. Arithmetic Instructionscntd
AAiT
IMUL can be used the same way for signed numbers taking care of
the sign bit.
ECEg - 4501 9
2. Arithmetic Instructionscntd AAiT
e.g.
SHL BX, 1 Shift word in BX 1 bit position left, 0 in LSB
MOV CL, 02H Load desired number of shifts in CL
SHL BP, CL Shift word in BP left (CL) bit positions, 0's in 2 LSBs
SHR AL, 4 Shift AL four bit positions right and put 0's in
upper 4 bits.
The AF, CF, OF, PF, SF, and ZF flags are affected by the comparison,
but neither of the operands is affected.
e.g.
MOV SI, OFFSET FIRST-STRING Point SI at source string
MOV DI, OFFSET SECOND-STRING Point DI at destination string
CLD DF cleared, so SI and DI will auto-increment after compare
MOV CX,100 Put number of string elements in CX
REPE Repeat the comparison of string bytes until end
CMPSB of string or until compared bytes are not equal
ECEg - 4501 14
5. Program Execution Instructions
AAiT
CALL subroutine
There are two basic types of calls, near and far.
Near: is a call to a procedure, which is in the same segment
as the CALL instruction.
Far: a call to a procedure, which is in a different segment from the
one that contains the CALL instruction.
JUMP instructions
JMP/JE/JZ/JC/JO/JA/JB/JG/JGE/JL/JLEaddress
Address: is the destination to jump to
Short jump: within the same segment; long jump: to a different seg.
e.g.
NEXT: CMP BX,DX ;Compare (BX-DX)
JGE DONE ;Jump to DONE if BX DX
SUB BX,AX ;Else subtract AX from BX
INC CX ;Increment counter
JMP NEXT ;Check again
DONE: MOV AX,CX ;Copy count to AX
IN AL, 8FH ;Read data from port 8FH
SUB AL,30H ;Subtract 30h from AX
JZ START_MACHINE ;Jump to label if result of subtraction was 0
ECEg - 4501 16
... Program Execution Instructions
AAiT
LOOP instructions
LOOP/LOOPE/LOOPNE label
ECEg - 4501 17
... Program Execution Instructions
AAiT
Interrupt instruction
INT type
Type: a number between 0 and 255 which identifies the interrupt.
ECEg - 4501 18
... Program Execution Instructions
AAiT
Interrupt instruction
e.g. (prints a number in binary on the console window)
..
mov al, 5 ; 05h, or 00000101b
mov bl, 10 ; 0Ah, or 00001010b
add bl, al ; 5 + 10 = 15 (decimal), 0Fh, or 00001111b
; print result in binary
mov cx, 8 ;for the 8 bits
print: mov ah, 2 ; print function.
mov dl, '0 ;Print the string hello world
test bl, 10000000b ; test first bit. msg db "hello world $
jz zero mov dx, offset msg
mov dl, '1 mov ah, 9
zero: int 21h ;print to console int 21h
shl bl, 1 ;wait for any key press
loop print mov ah, 1
; print binary suffix: int 21h
mov dl, b ret ;return to the operating system
int 21h
ECEg - 4501 19
6. Processor control Instructions AAiT
ECEg - 4501 20