Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microprocessor and Assembly Language

Similar presentations


Presentation on theme: "Microprocessor and Assembly Language"— Presentation transcript:

1 Microprocessor and Assembly Language
Lecture-5-Program Structure of Assembly Language Muhammad Hafeez Department of Computer Science GC University Lahore

2 Today’s Agenda Program Structure Assembler Directives

3 Memory Models Memory Models defines how much memory we need for a program TINY SMALL MEDIUM COMPACT LARGE HUGE

4 Memory Models TINY SMALL MEDIUM COMPACT LARGE HUGE
64K for both code and data segment SMALL 64K for code segment 64K for data segment MEDIUM 64K data segment Code can exceed 64K COMPACT data segment can exceed 64K Code segment 64K LARGE Data can exceed 64K but Array declared in one data segment can not exceed 64K HUGE Data can exceed 64K, Array declared in one data segment can exceed 64K

5 Memory Models

6 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

7 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

8 Assembly Language Statements

9 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

10 Assembly Language Program

11 Data Movement Instructions
simplified Assigns a Title to your Source File Stack of program Data of program Label Assembler Directive Comment

12 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]

13 Data Defining Directives

14 Declaration of Data DATA1 DB 15H DATA2 DB 15H, 1FH, 4AH
DATA3 DB ? ;SET ASIDE ONE BYTE DATA4 DB ‘STRING OF CHARACTERS’,’$’ DB could be replaced with DW, DD, DQ or DT depending upon requirement

15 Named Constants with Directive “EQU”
To assign a name to constant, use EQU pseudo-op EQU Syntax Name EQU Constant Example: LF EQU 0AH CR EQU 0DH PROMPT EQU ‘Enter Number’ No Memory is allocated for EQU pseudo-op

16 More Data Definition Directives
To assign a name to constant, use EQU pseudo-op EQU Syntax Name EQU Constant Example: LF EQU 0AH CR EQU 0DH PROMPT EQU ‘Enter Number’ No Memory is allocated for EQU pseudo-op

17 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.

18 Assemble, Link and Run a Program

19 Files Created by Assembler
The Source Listing File (.LST) Display line number and corresponding machine code side-by-side, helpful in debugging The Cross Reference File (.CRF) List of variables and labels in the programs

20 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]

21 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

22 Flag Register

23 Conditional Flags The processor uses conditional flags to make decision SUB AX, AX set Zero Flag = 1 CARRY FLAG (CF): 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)

24 Conditional Flags Auxiliary Carry (AF): Zero Flag (ZF):
AF=1, If carry out from D3 bit to D4 bit, otherwise AF=0 Zero Flag (ZF): ZF=1, If result is zero. ZF=0, for non-zero results Sign Flag (SF): 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

25 Control Flags Trap Flag (TF): Interrupt Flag (IF):
TF=1, Allow to program to execute in single step, helpful in debugging TF=0, otherwise Interrupt Flag (IF): IF=1, Allow external maskable interrupts to the processor IF=0, otherwise Direction Flag (DF): Used to control the direction of string operations

26 Flag Register in Debug Utility
Command is --- R F

27 Instructions Affect Flags
MOV/ XCHG None ADD/ SUB All INC/ DEC 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

28 Examples: AX=FFFFH, BX=FFFFH, ADD AX,BX
SF=1, CF=1, AF=1 MOV AL,80H, MOV BL,80H, ADD AL,BL 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

29 Examples: Addition 0Fh + 08h? Addition 0Fh + F8h? Addition 4Fh + 40h?
Represent in signed and unsigned numbers

30 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

31 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

32 Some Basic I/O Operations
Read a Character Display a Character Display a String

33 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

34 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

35 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

36 Questions ??????????????????????????


Download ppt "Microprocessor and Assembly Language"

Similar presentations


Ads by Google