Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
MICROPROCESSOR AND ASSEMBLY LANGUAGE LECTURE-5-PROGRAM STRUCTURE LANGUAGE MUHAMMAD HAFEEZ DEPARTMENT OF COMPUTER SCIENCE GC UNIVERSITY LAHORE OF ASSEMBLY TODAY’S AGENDA Program Structure Assembler Directives MEMORY MODELS Memory Models defines how much memory we need for a program TINY SMALL MEDIUM COMPACT LARGE HUGE MEMORY MODELS TINY SMALL data segment can exceed 64K Code segment 64K LARGE 64K data segment Code can exceed 64K COMPACT 64K for code segment 64K for data segment MEDIUM 64K for both code and data segment Code can exceed 64K Data can exceed 64K but Array declared in one data segment can not exceed 64K HUGE Code can exceed 64K Data can exceed 64K, Array declared in one data segment can exceed 64K MEMORY MODELS ASSEMBLY LANGUAGE STATEMENTS Assembly Language Statements are either, Instructions Directive (also called Pseudo-Ops) Both Instruction and Directive can have up to 4 Fields [Label] mnemonic [operand] [;comment] Brackets indicate the fields are optional ASSEMBLY LANGUAGE STATEMENTS Label: 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 As it tells the assembler to refer the code ASSEMBLY LANGUAGE STATEMENTS ASSEMBLY LANGUAGE STATEMENTS Mnemonic: Instructions and Operands for Processor Real work of program MOV AX,5 ADD AX,BX Pseudo-Ops/ Directive for Assembler MAIN ENDP DB Comment: 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 Storage is defined using data definition directives Storage is created at assembly time. Variables can be initialized to character string Syntax Name Data Directive Initial value, [values] DATA DEFINING DIRECTIVES DECLARATION OF DATA DATA1 DATA2 DATA3 DATA4 DB DB DB DB 15H 15H, 1FH, 4AH ? ;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” To assign a name to constant, use EQU pseudo-op EQU Syntax EQU Constant EQU EQU 0AH 0DH Example: LF CR PROMPT EQU Name ‘Enter Number’ No Memory is allocated for EQU pseudo-op MORE DATA DEFINITION DIRECTIVES To assign a name to constant, use EQU pseudo-op EQU Syntax EQU Constant EQU EQU 0AH 0DH Example: LF CR PROMPT EQU Name ‘Enter Number’ No Memory is allocated for EQU pseudo-op ASSEMBLE, LINK AND RUN A PROGRAM Software you need can be downloaded from blog, along with instructions to set up your assembly language programming environment. Three Steps are required to create, assemble and link an assembly language program 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 PRG1.EXE (Enter) .. U CS:0 1 to find data segment address, D Datasegment: 0 F , G, Then again D DataSegment: 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 The Source Listing File (.LST) The Cross Reference File (.CRF) Display line number and corresponding machine code sideby-side, helpful in debugging List of variables and labels in the programs The Cross Reference File (.CRF) List of variables and labels in the programs MORE DIRECTIVE OF MASM FOR HANDLING DATA ORG [Number] Is used to mark the beginning of offset address ORG 10H OFFSET Returns the offset of a variable from the start of its segment address The destination must be 16-bit MOV BX, OFFSET DATA2 [More on Data Definitions, PTR, SEG later] FLAG REGISTER Flag Register defines processor status and help it to make decisions Decision making is done by looking at current state of processor Nine individual bits called FLAGS in flag register represents a particular state of processor Flags are classified as control flags and status flags Status flags are affected by result of computations, also called conditional flags FLAG REGISTER CONDITIONAL FLAGS The processor uses conditional flags to make decision CARRY FLAG (CF): SUB AX, AX set Zero Flag = 1 CF=1, If carry out from MSB (D7) for Byte, (D15) for Word in case of addition, or Borrow in MSB in case of subtraction, also affected by rotate instructions Parity Flag (PF): 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 Auxiliary Carry (AF): Zero Flag (ZF): ZF=1, If result is zero. ZF=0, for non-zero results Sign Flag (SF): AF=1, If carry out from D3 bit to D4 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): OF=1, If Singed overflow occurs OF=0, otherwise CONTROL FLAGS Trap Flag (TF): Interrupt Flag (IF): 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): Used to control the direction of string operations FLAG REGISTER IN DEBUG UTILITY Command is --- R F INSTRUCTIONS AFFECT FLAGS MOV/ XCHG ADD/ SUB All INC/ DEC None All except CF NEG All, (By default set CF=1, only exception result is zero) OF=1, if Word operand is 8000H and byte operand is 80H EXAMPLES: AX=FFFFH, BX=FFFFH, ADD AX,BX MOV AL,80H, MOV BL,80H, ADD AL,BL SF=1, CF=1, AF=1 PF=1, ZF=1, CF=1, OF=1, SF=0 MOV CX, 5 DEC CX, DEC CX, DEC CX, DEC CX, DEC CX After execution of these instructions result will be zero hence, ZF=1 EXAMPLES: Addition 0Fh + 08h? Addition 0Fh + F8h? Addition 4Fh + 40h? Addition F8h + 81h? Represent in signed and unsigned numbers SOME BASIC I/O OPERATIONS INT 21H , 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 INT 21H , 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 Read a Character Display a Character Display a String SOME BASIC I/O OPERATIONS Read a Character INT 21h / AH=1 - read character from standard input, with echo, result is stored in AL. If there is no character in the keyboard buffer, the function waits until any key is pressed. Example: mov ah, 1 int 21h SOME BASIC I/O OPERATIONS Display a Character INT 21h / AH=2 - write character to standard output. DL = character to write, after execution AL = DL. Example: mov ah, 2 mov dl, ’a’ int 21h SOME BASIC I/O OPERATIONS Display a String INT 21h / AH=9 - display a string character string on the console. The offset of string must be in DX string must be end with $ (24h) mov ah,9 ;string output function mov dx,offset msg ;offset address of the string int 21 ;call DOS obvious disadvantage, a dollar sign can not be displayed as part of a string QUESTIONS ??????????????????????????