Microprocessors & Interfacing Assembly Programming Fundamentals Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming Introduction Machine language: is a numeric language (‘0’s and ‘1’s) specifically understood by a computer’s processor (the CPU). Assembly language: is a series of statements which are either are Instructions or Directives. Assembly language: has a one-to-one relationship with machine language. Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming Directives Directives: are statements that give directions to the assembler about how it should translate the assembly language instructions into machine code. Directives: are used by the assembler to organize the program as well as other output files. SEGMENT, DB, ENDS, ASSUME, END, and ENDP are examples of directives. Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming Instructions Instruction: an assembly language instruction consists of four fields: [label:] mnemonic [operands] [;comments] Brackets indicate that the field is optional. Brackets are not typed. The label field allows the program to refer to a line of code by name. In a line of assembly language program there can be mnemonic (instruction) and operand(s). Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming Instruction format Instructions: Examples: MOV AL,BL ; ADD AL,16H ; MOV and ADD are mnemonic opcodes, and AL, BL, and 16H are the operands. Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming MOV Instruction MOV Instruction: MOV destination, source; copy source operand to destination Mnemonic Dr. Abduljalil Radman Comment Operands Microprocessors & Interfacing Taiz University 2020 Assembly Programming MOV Instruction • These types of operands are supported: MOV REG, Memory MOV Memory, REG MOV REG, REG MOV Memory, constant MOV REG, Constant REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP. Memory: [BX], [BX+SI+7], variable, etc... Constant: 5, -24, 3Fh, 10001101b, etc... Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming MOV Instruction Examples: MOV CL,16H ; register, constant MOV DL,CL ; register, register MOV BH,25-5 ; register, constant-expression MOV BL,[010A] ; register, memory MOV CX,468FH ;move 468FH into CX (CH =46 , CL=8F) MOV AX,CX ;move/copy the contents of CX into AX MOV BX,AX ;BX=AX=468FH MOV DX,BX ;DX=BX=468FH MOV DI,AX ;DI=AX=468FH MOV SI,DI ;SI=DI=468FH MOV DS,SI ;DS=SI=468FH MOV BP,DS ;BP=DS=468FH Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming Rules Regarding MOV Instruction • Data can be moved among all registers except the flag register. • Source and destination registers must be in the same size. • Data can be moved directly into non segment registers only. You can’t move data to segment registers directly. Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming MOV Instruction Example: Dr. Abduljalil Radman MOV BX,A45DH MOV SI,ADCFH MOV CS,23AFH MOV DS,BX Microprocessors & Interfacing Taiz University 2020 Assembly Programming Adding and Subtracting Integers Example: Dr. Abduljalil Radman MOV AL,5 ; move 5 to the AL register. ADD AL,6 ; add 6 to the AL register. SUB AL,2 ; sub 2 from the AL register. Microprocessors & Interfacing Taiz University 2020 Assembly Programming MOV Instruction • For segment registers, only these types of MOV are supported: MOV MOV MOV MOV SREG, memory memory, SREG REG, SREG SREG, REG SREG: DS, ES, SS, and only as source operand: CS. REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP. Memory: [BX], [BX+SI+7], variable, etc... The MOV instruction cannot be used to set the value of the CS and IP registers. Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming MOV Instruction Example: MOV AX, 0B800H ; set AX to hexadecimal value of B800h. MOV DS, AX ; copy value of AX to DS. MOV CL, 'A’ ; set CL to ASCII code of 'A', it is 41H. MOV CH, 01011111b ; set CH to binary value. MOV BX, 15Eh ; set BX to 15Eh. MOV [BX], CX ; copy contents of CX to memory at B800:015E Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming Full Segment Definition SEGMENT and ENDS directives: indicate the beginning and ending of a segment and have the following format: label SEGMENT [options] ; place the statements belong to this segment here label ENDS Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming Full Segment Definition • Stack Segment Definition STSEG SEGMENT DB 64 DUP (?) ;directive reserves 64 bytes of memory for the Stack STSEG ENDS • Data Segment Definition There are three data items in this sample program: DATA1, DATA2 and SUM. Each is defined as DB (Define Byte). The DB directive is used to allocate memory in byte sized chunks. DW (Define Word) allocates 2 bytes of memory. DATA1 and DATA2 have initial values but SUM is reserved for later use. Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming Full Segment Definition • Code segment definition The first line after the SEGMENT directive is the PROC directive. A procedure is a group of instructions designed to accomplish a specific function. The PROC and ENDP directives must have the same label. The PROC directive may have the option FAR or NEAR . ASSUME directive associates segment registers with specific segments by assuming that the segment register is equal to the segment labels used in the program. Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming Simplified Segment Definition • The directives .stack, .code, and .data are used to identify the three different segments. • With this definition, the .MODEL directive is introduced to specify the memory model used. The common used memory model is SMALL (one segment for Data, and one segment for code). • The .MODEL directive should be before any segment definition. Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming Simplified Segment Definition Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming Full and Simplified Segment Definition Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming Assembler Data Directives • DB : Define Byte. Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming Assembler Data Directives • DUP : Duplicate. Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming Assembler Data Directives • DW : Define Word. Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming Assembler Data Directives • DD : Define Double Word. Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Stack Why and how to be accessed • The stack: is a section of memory, used by the CPU to store information temporarily: The SS (stack segment) register The SP (stack pointer) register These registers must be loaded before any instructions accessing the stack are used. • Storing a CPU register in the stack is called a PUSH. • Loading the contents of the stack into the CPU register is called a POP. Logical Address in Stack Segment is represented by using segment address in SS register and Offset Address in SP register: SS:SP. Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming PUSH and POP Instruction format PUSH source ; Copy the content of source into the stack Mnemonic operand comment POP destination ; Copy the content of destination into the stack Mnemonic Dr. Abduljalil Radman operand comment Microprocessors & Interfacing Taiz University 2020 Assembly Programming PUSH Example: Assuming that SP = 1236 H , AX = 24B6 H , DI = 85C2 H , and DX = 5F93 H , show the contents of the stack as each of the following instructions is executed: PUSH PUSH PUSH Dr. Abduljalil Radman AX DI DX Microprocessors & Interfacing Taiz University 2020 Assembly Programming PUSH Solution: *Note that in 80x86 the lower byte of the register is stored to the lower address. (Little Endian Convention). Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming POP Example: Assuming that SP = 18FA H , show the contents of the stack as each of the following instructions is executed: POP POP POP CX DX BX Last In First out (LIFO) *Note that in 80x86 the lower byte of the register is stored to the lower address. (Little Endian Convention). Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020 Assembly Programming Flag register There are two instructions that can be used to use/change the content of the flag register. PUSHF POPF ; Copy the flag register into stack ; Copy from the stack into the flag register Example: PUSHF POPF Dr. Abduljalil Radman ; copy the content of Flag register into the stack. ; Microprocessors & Interfacing Taiz University 2020 Assembly Programming 8086 EMULATOR Dr. Abduljalil Radman Microprocessors & Interfacing Taiz University 2020