MICROPROCESSOR AND ASSEMBLY LANGUAGE LECTURE5 PROGRAM STRUCTURE OF
MICROPROCESSOR AND ASSEMBLY LANGUAGE LECTURE-5 -PROGRAM STRUCTURE OF ASSEMBLY LANGUAGE MUHAMMAD HAFEEZ DEPARTMENT OF COMPUTER SCIENCE GC UNIVERSITY LAHORE
TODAY’S AGENDA l l Program Structure Assembler Directives
MEMORY MODELS l Memory Models defines how much memory we need for a program l l l TINY SMALL MEDIUM COMPACT LARGE HUGE
MEMORY MODELS l TINY l l SMALL l l l data segment can exceed 64 K Code segment 64 K LARGE l l l 64 K data segment Code can exceed 64 K COMPACT l l 64 K for code segment 64 K for data segment MEDIUM l l 64 K for both code and data segment Code can exceed 64 K Data can exceed 64 K but Array declared in one data segment can not exceed 64 K HUGE l l Code can exceed 64 K Data can exceed 64 K, Array declared in one data segment can exceed 64 K
MEMORY MODELS
ASSEMBLY LANGUAGE STATEMENTS l Assembly Language Statements are either, l l Instructions Directive (also called Pseudo-Ops) l Both Instruction and Directive can have up to 4 Fields [Label] mnemonic [operand] [; comment] l Brackets indicate the fields are optional l
ASSEMBLY LANGUAGE STATEMENTS l Label: l l l l Label composed of A-Z, a-z, 0 -9 and special character ? , . , @, $ Must begin with an alphabetic and special character Cannot exceed 31 characters. Can only be used as first character No Case Sensitivity Label for directive do not need to end with Colon (: ) Label for Instruction needs to end with (: ) colon l As it tells the assembler to refer the code
ASSEMBLY LANGUAGE STATEMENTS
ASSEMBLY LANGUAGE STATEMENTS l Mnemonic: l l l l l Instructions and Operands for Processor Real work of program MOV AX, 5 ADD AX, BX Pseudo-Ops/ Directive for Assembler MAIN ENDP DB Comment: l l At end of each line Optional BUT Highly important for readable/ understandable Assembly Language Programs
ASSEMBLY LANGUAGE PROGRAM
DATA MOVEMENT INSTRUCTIONS simplified Assigns a Title to your Source File Label Stack of program Data of program Assembler Directive Comment
DATA DEFINING DIRECTIVES l Storage is defined using data definition directives Storage is created at assembly time. Variables can be initialized to character string l Syntax l Name Data Directive Initial value, [values]
DATA DEFINING DIRECTIVES
DECLARATION OF DATA l l l DATA 1 DATA 2 DATA 3 DATA 4 DB DB 15 H, 1 FH, 4 AH ? ; SET ASIDE ONE BYTE ‘STRING OF CHARACTERS’, ’$’ DB could be replaced with DW, DD, DQ or DT depending upon requirement
NAMED CONSTANTS WITH DIRECTIVE “EQU” l To assign a name to constant, use EQU pseudo-op l EQU Syntax l l EQU Constant EQU 0 AH 0 DH Example: l LF CR l PROMPT EQU l l Name ‘Enter Number’ No Memory is allocated for EQU pseudo-op
MORE DATA DEFINITION DIRECTIVES l To assign a name to constant, use EQU pseudo-op l EQU Syntax l l EQU Constant EQU 0 AH 0 DH Example: l LF CR l PROMPT EQU l l Name ‘Enter Number’ No Memory is allocated for EQU pseudo-op
ASSEMBLE, LINK AND RUN A PROGRAM l Software you need can be downloaded from blog, along with instructions to set up your assembly language programming environment. l Three Steps are required to create, assemble and link an assembly language program l l l Step 1: A simple Text Editor to create assembly language program file and save it with Extension (. ASM) An Assembler MASM (Microsoft Macro Assembler) that takes (. ASM) file and create an Object File with Extension (. OBJ) A Linker Program (LINK) that takes an (. OBJ) file and create Executable file with Extension (. EXE) Run Executable File Execute under Debug, DEBUG PRG 1. EXE (Enter). . U CS: 0 1 to find data segment address, D Datasegment: 0 F , G, Then again D Data. Segment: 0 F Note: Other assembler and linker program also exist, like Borland’s TASM and TLINK etc.
ASSEMBLE, LINK AND RUN A PROGRAM
FILES CREATED BY ASSEMBLER l The Source Listing File (. LST) l l The Cross Reference File (. CRF) l l Display line number and corresponding machine code side-byside, helpful in debugging List of variables and labels in the programs The Cross Reference File (. CRF) l List of variables and labels in the programs
MORE DIRECTIVE OF MASM FOR HANDLING DATA l ORG [Number] l l l Is used to mark the beginning of offset address ORG 10 H OFFSET l l l Returns the offset of a variable from the start of its segment address The destination must be 16 -bit MOV BX, OFFSET DATA 2 [More on Data Definitions, PTR, SEG later]
FLAG REGISTER l Flag Register defines processor status and help it to make decisions l Decision making is done by looking at current state of processor l Nine individual bits called FLAGS in flag register represents a particular state of processor l Flags are classified as control flags and status flags Status flags are affected by result of computations, also called conditional flags l
FLAG REGISTER
CONDITIONAL FLAGS l The processor uses conditional flags to make decision l l CARRY FLAG (CF): l l SUB AX, AX set Zero Flag = 1 CF=1, If carry out from MSB (D 7) for Byte, (D 15) for Word in case of addition, or Borrow in MSB in case of subtraction, also affected by rotate instructions Parity Flag (PF): l l PF=1, If low byte of a result has even number of 1 bits (even parity). PF=0, if low byte of a result has odd number of 1 bits (odd parity)
CONDITIONAL FLAGS l Auxiliary Carry (AF): l l Zero Flag (ZF): l l l ZF=1, If result is zero. ZF=0, for non-zero results Sign Flag (SF): l l l AF=1, If carry out from D 3 bit to D 4 bit, otherwise AF=0 SF=1, If after computations MSB=1, if you are giving a signed interpretation to result SF=0, if MSB = 0 Overflow Flag (OF): l l OF=1, If Singed overflow occurs OF=0, otherwise
CONTROL FLAGS l Trap Flag (TF): l l l Interrupt Flag (IF): l l l TF=1, Allow to program to execute in single step, helpful in debugging TF=0, otherwise IF=1, Allow external maskable interrupts to the processor IF=0, otherwise Direction Flag (DF): l Used to control the direction of string operations
FLAG REGISTER IN DEBUG UTILITY l Command is --- R F
INSTRUCTIONS AFFECT FLAGS l MOV/ XCHG l l ADD/ SUB l l All INC/ DEC l l None All except CF NEG l l All, (By default set CF=1, only exception result is zero) OF=1, if Word operand is 8000 H and byte operand is 80 H
EXAMPLES: l AX=FFFFH, BX=FFFFH, ADD AX, BX l l MOV AL, 80 H, MOV BL, 80 H, ADD AL, BL l l SF=1, CF=1, AF=1 PF=1, ZF=1, CF=1, OF=1, SF=0 MOV CX, 5 l l DEC CX, DEC CX After execution of these instructions result will be zero hence, ZF=1
EXAMPLES: l l Addition 0 Fh + 08 h? Addition 0 Fh + F 8 h? Addition 4 Fh + 40 h? Addition F 8 h + 81 h? l Represent in signed and unsigned numbers
SOME BASIC I/O OPERATIONS l l l INT 21 H , an interrupt also called DOS function call 87 different interrupts are supported by this DOS function call Each interrupt is identified by a function number placed in AH register
SOME BASIC I/O OPERATIONS l l l INT 21 H , an interrupt also called DOS function call 87 different interrupts are supported by this DOS function call Each interrupt is identified by a function number placed in AH register
SOME BASIC I/O OPERATIONS l l l Read a Character Display a String
SOME BASIC I/O OPERATIONS Read a Character l INT 21 h / AH=1 - read character from standard input, with echo, result is stored in AL. l If there is no character in the keyboard buffer, the function waits until any key is pressed. Example: mov ah, 1 int 21 h l
SOME BASIC I/O OPERATIONS Display a Character l INT 21 h / AH=2 - write character to standard output. l DL = character to write, after execution AL = DL. Example: l mov ah, 2 l mov dl, ’a’ l int 21 h l
SOME BASIC I/O OPERATIONS Display a String l INT 21 h / AH=9 - display a string character string on the console. l The offset of string must be in DX l string must be end with $ (24 h) mov ah, 9 ; string output function mov dx, offset msg ; offset address of the string int 21 ; call DOS l obvious disadvantage, a dollar sign can not be displayed as part of a string l
QUESTIONS l ? ? ? ? ? ? ?
- Slides: 36