Transcript Lab Work 2
MICROPROCESSOR
INTRODUCTION TO ASSEMBLY PROGRAMMING
LANGUAGE
Yrd. Doç. Dr. Abdül Kadir Görür
Uğur Sopaoğlu
REGISTERS
• REGISTERS CAN BE GROUPED INTO GENERAL, CONTROL AND SEGMENT REGISTERS
• GENERAL REGISTERS
•
DATA REGISTER : AX, BX,CX, DX => 16 BITS
•
POINTER AND INDEX REGISTERS: SI, DI, SP, BP
• CONTROL REGISTERS
•
INSTRUCTION POINTER REGISTER AND FLAG REGISTER
•
STATUS FLAG REGISTERS => CARRY FLAG (CF), PARITY FLAG (PF), AUXILIARY FLAG(AF), ZERO FLAG (ZF), SIGN FLAG(SF),
OWERFLOW FLAG (OF)
•
CONTROL FLAGS => DIRECTION FLAG(DF)
•
SYSTEM FLAGS => TF, IF, IOPL, NT, RF, VM, AC, VIF, VIP, ID
REGISTERS
• SEGMENT REGISTERS
• CODE SEGMENT REGISTER(CS)
• DATA SEGMENT REGISTER(DS)
• STACK SEGMENT REGISTER(SS)
• ES, GS AND FS ( ADDITIONAL SEGMENT REGISTERS)
• LABEL
MNEMONIC
OPERAND
COMMENT
--------------------------------------------------------.DATA
EXCODE
DB
0
;A BYTE VARIABLE
MYWORD
DW
?
;UNINITIALIZED WORD VAR.
.CODE
MAIN
PROC
MOV
AX,@DATA
;INITIALIZE DS TO ADDRESS
MOV
DS,AX
; OF DATA SEGMENT
JMP
EXIT
;JUMP TO EXIT LABEL
MOV
CX,10
;THIS LINE SKIPPED!
EXIT:
MOV
AH,04CH
;DOS FUNCTION: EXIT PROG
MOV
AL, EXCODE
;RETURN EXIT CODE VALUE
INT
21H
;CALL DOS. TERMINATE PROG
MAIN
ENDP
;END PROGRAM
END
MAIN
; AND SPECIFY ENTRY POINT
DATA ALLOCATION
• DATA CAN BE DECLARED WITH FOLLOWING SIZE
• DB
DEFINE BYTE => 1 BYTE
• DW
DEFINE WORD=> 2 BYTES
• DD
DEFINE DOUBLEWORD=> 4 BYTES
• DQ
DEFINE QUADWORD=>8 BYTES(EMU8086 DOES NOT SUPPORT)
• DT
DEFINE TEN BYTES=> 10 BYTES(EMU8086 DOES NOT SUPPORT)
EXAMPLES
• SORTED DB ‘Y’
• SORTED DB ? => UNINITIALIZED VARIABLE
• VALUE DW 251
• VALUE DW -251 => YOU CAN ALSO ASSIGN NEGATIVE VALUES
WE CAN GENERALLY USE DD AND DQ DIRECTIVES TO ASSIGN REAL NUMBERS
FLOAT DD 1.234
REAL DQ 123.456
MULTIPLE DEFINITION AND INITIALIZATIONS
•
MESSAGE DB ‘WELCOME!’
•
MARKS DB 0,1,1,0,0,0,0,1,1,0
•
MARKS DB 10 DUP(0)
•
MARKS DB 10(?)
•
MARKS DB 10(‘?’)
CORRESPONDENCE TO C TYPES
INVALID MOV OPERATIONS
• MOV DL, CX
DIFFERENT OPERAND SIZES
• MOV DS, 175
IMMEDIATE VALUE CAN NOT BE MOVED INTO A
SEGMENT REGISTER
• MOV 715, AX
IMMEDIATE VALUE CAN NOT BE DESTINATION
OPERAND
• MOV ES, DS
BOTH REGISTER CAN NOT BE SEGMENT REGISTERS
• MOV CS, ES
BOTH REGISTER CAN NOT BE SEGMENT REGISTERS
AMBIGUOUS MOVES: PTR DIRECTIVE
• MOV BX, OFFSET TABLE1
• MOV [BX],100
• THIS OPERATION IS NOT A CLEAR
• MOV WORD PTR [BX], 100
• PTR DIRECTIVE CAN BE USED TO CLARIFY
• INC [BX] AND DEC [BX]
• THESE OPERATIONS ARE AMBIGUOUS
• INC WORD PTR [BX] AND DEC WORD PTR [BX]
ADD, SUB AND CMP
• ADD DESTINATION, SOURCE
• ADD AX,BX => AX = AX + BX
• SUB DESTINATION, SOURCE
• SUB AX,BX => AX = AX-BX
• CMP (COMPARE) INSTRUCTION IS USED TO COMPARE TWO OPERANDS. THE CMP INSTRUCTION THE
PERFORMS SAME OPERATION AS THE SUB EXCEPT THAT THE RESULT OF SUBTRACTION IS NOT
SAVED
• NOT IS USED TO TAKE ONE’S COMPLEMENT OF ANY NUMBER
• NEG IS USED TO TAKE TWO’S COMPLEMENT OF ANY NUMBER
JUMP OPERATION
• UNCONDITIONAL JUMP
•
•
MOV AX, 1
INC_AGAIN:
•
INC AX
•
JMP INC_AGAIN
• CONDITIONAL JUMP
•
•
MOV AX,4
•
INC AX
•
CMP AX,5
•
JE A
A:
JUMP OPERATION
• JE
JUMP IF EQUAL
• JG
JUMP IF GREATER
• JL
JUMP IF LESS
• JGE
JUMP IF GREATER THAN OR EQUAL
• JLE
JUMP IF LESS THAN OR EQUAL
• JNE
JUMP IF NOT EQUAL
ITERATION INSTRUCTION(LOOP)
• MOV CL,50
• REPEAT:
• <LOOP BODY>
• DEC CL
• JNZ REPEAT
LOGICAL INSTRUCTIONS
• AND DESTINATION, SOURCE
• OR
DESTINATION, SOURCE
• NOT DESTINATION
• HOW CAN WE CHECK THE VALUE OF SIGN BIT?
SHIFT INSTRUCTION
• SHIFT LEFT(SHL) OR SHIFT RIGHT(SR) CAN BE USED
• SHL DESTINATION, COUNT
• SHR DESTINATION, COUNT
MOV AX,001100
SHL AX,2 => NEW VALUE OF AX = 110000
ROTATE INSTRUCTIONS
• ROTATE WITHOUT CARRY
• ROTATE LEFT(ROL), ROTATE RIGHT(ROR)
• ROL DESTINATION, COUNT
• MOV AL, 1010 1110B
• ROL AL,1 => THE NEW VALUE OF AL : 0101 1101B
• MOV AL, 10101110B
• ROR AL,1 = THE NEW VALUE OF AL: 0101 0111B
ROTATE INSTRUCTIONS
• ROTATE THROUGH CARRY
• INSTRUCTIONS RCL AND RCR INCLUDE THE CARRY FLAG IN THE ROTATION PROCESS.
• MOV AL, 1010 1110B
• RCL AL,1 => THE NEW VALUE OF AL: 0101 1100
• RCR AL,1 => THE NEW VALUE OF AL: 11010111
• BUT IN EMU8086, THERE IS NO DIFFERENCE BETWEEN ROR – RCR AND ROL-RCL
DEFINING CONSTANT
• TO DEFINE CONSTANT NUMERIC OR STRING, EQU DIRECTIVE IS USED
• NAME EQU EXPRESSION
• FOR EXAMPLE;
• NUM_OF_STUDENTS EQU 90
• IF ANY VALUE IS DEFINED WITH EQU, THE VALUE CAN NOT BE CHANGED DURING THE
PROGRAM
• ALSO IT CAN BE USED AS MNEMONIC :
• JUMP EQU JMP
QUESTION
• WRITE AN ASSEMBLY PROGRAM. IN THE PROGRAM, YOU MUST DEFINE A WORD ARRAY SIZE
OF 10. INITIAL VALUE OF THE EACH ITEM OF ARRAY IS ZERO. THEN, IN THE PROGRAM, CHANGE
THE VALUE OF ARRAY ITEM FROM 0 TO 9. FINALLY FIND THE SUMMATION OF ITEM WHOSE
LEAST SIGNIFICANT BIT IS ZERO.