BACKGROUND Segment The "SEGMENT" and "ENDS" directives indicate to the assembler the beginning and ending of a segment and have the following format label SEGMENT [options] ;place the statements belonging to this segment here label ENDS The [options] field gives important information to the assembler for organizing the segment, but is not required. The ENDS label must be the same label as in the SEGMENT directive. In the full segment definition, the ".MODEL" directive is not used. Further, the directives ".STACK", ".DATA", and ".CODE" are replaced by SEGMENT and ENDS directives that surround each segment. The Assembly Programming Language consists of three types of segments Code segment contains the program code (instructions, commands) Stack segment is used to temporarily store information. Data segment is used to store the data (information) to be processed by the program Logical and Physical Address Physical Address is the 20-bit address that actually put on the address bus. (in 8086) Has a range of 00000H FFFFFH Offset Address is a location within 64K byte segment range Has a range of 0000H FFFFH Logical Address consists of segment address and offset address. Addressing in Code segment 8086 fetches the instructions from the code segment in order to execute a program. The logical address of an instruction consists of CS (Code Segment) and IP(instruction pointer) Physical Address is generated by shifting the CS one hex digit to the left and adding IP. Example CSIP is 250095F3H Start with CS 2500 Shift left CS 25000 Add IP 2E5F3 (25000+95F3) The microprocessor will retrieve the instruction in turn memory locations starting from 2E5F3.
Example If CS=24F6H and IP=634AH 2016-2017 SPRING TERM The logical address The offset address The physical address The lower range of the code segment The upper range of the code segment 24F6634A 634A 24F60+634A= 2B2AA 24F60000 => 24F60+0000 =24F60 24F6FFFF => 24F60+FFFF=34F5F Addressing in Data segment The area of memory allocated strictly for data is called data segment. Just as the code segment is associated with CS and IP as segment register and offset. The data segment uses DS and an offset value. In 8086 BX, SI and DI are used to hold the offset address. Example If DS= 7FA2H and the offset is 438EH The physical address The lower range of the data segment The upper range of the data segment Show the logical address 7FA20+438E= 83DAE 7FA20(7FA20+0000) 8FA1F(7FA20+FFFF) 7FA2438E Subroutine A subroutine is a group of instructions that will be used repeatedly in different locations of the program. Rather than repeat the same instructions several times, they can be grouped into a subroutine that is called from the different locations. In Assembly language, a subroutine can exist anywhere in the code. However, it is customary to place subroutines separately from the main program. The CALL instruction is a special branch instruction and performs the following operations Push the contents of the PC on the top of the stack Update the stack pointer Branch to the target address specified by the instruction The URN instruction is a special branch instruction that performs the following operations Pop the return address from the top of the stack into the PC Update the stack pointer.
Assembler data directives 2016-2017 SPRING TERM All the assemblers designed for the 80x86 (8088, 8086, 80188, 80186, 80286, 80386, 80386SX, 80486, and Pentium) microprocessors have standardized the directives for data representation. The following are some of the data directives used by the 80x86 microprocessor and supported by all software and hardware vendors of IBM PCs and compatibles. ORG Origin DB Define Byte DUP Duplicate DW Define Word EQU Equate DD Define Doubleword DQ Define Quadword DT Define Ten Bytes ORG is used to indicate the beginning of the offset address. It allows allocation of memory in byte-sized chunks. DUP is used to duplicate a given number of characters. This can avoid a lot of typing. DW is used to allocate memory 2 bytes (one word) at a time. This is used to define a constant without occupying a memory location. The DD directive is used to allocate memory locations that are 4 bytes (two words) in size. DQ is used to allocate memory 8 bytes (four words) in size. DT is used for memory allocation of packed BCD numbers. Instruction Operand(s) Description Load Effective Address. REG = address of memory (offset) LEA LDS REG,memory REG,memory MOV BX, 35h MOV DI, 12h LEA SI, [BX+DI]; SI = 35h + 12h = 47h The integrated 8086 assembler automatically replaces LEA with a more efficient MOV where possible. org 100h LEA AX, m ; AX = offset of m m dw 1234h END Load memory double word into word register and DS. REG = first word DS = second Word ORG 100h LDS AX, m m DD 12345678h END
Load memory double word into Word register and ES. REG = first word ES = second Word LES XCHG XLATB LAHF SAHF POPF PUSHF REG,memory REG, memory memory, REG REG, REG No operands No operands No operands No operands No operands ORG 100h LES AX, m m DW 1234h DW 5678h END Exchange values of two operands. operand1 operand2 MOV AL, 5 MOV AH, 2 XCHG AL, AH ; AL = 2, AH = 5 XCHG AL, AH ; AL = 5, AH = 2 Translate byte from table. Copy value of memory byte at DS[BX + unsigned AL] to AL register. AL = DS[BX + unsigned AL] ORG 100h LEA BX, dat MOV AL, 2 XLATB ; AL = 33h dat DB 11h, 22h, 33h, 44h, 55h Load AH from 8 low bits of Flags register. AH = flags register AH bit 7 6 5 4 3 2 1 0 [SF] [ZF] [0] [AF] [0] [PF] [1] [CF] bits 1, 3, 5 are reserved. Store AH from 8 low bits of Flags register. AH = flags register AH bit 7 6 5 4 3 2 1 0 [SF] [ZF] [0] [AF] [0] [PF] [1] [CF] bits 1, 3, 5 are reserved. Get flags register from the stack. flags = SS[SP] (top of the stack) SP = SP + 2 Store flags register in the stack. SP = SP 2 SS[SP] (top of the stack) = flags
Laboratory Preliminary Study 2016-2017 SPRING TERM Q1 The following program code separates the odd numbers and the even numbers of an array stored in memory. Please run the program step by step and observe its execution. org 0100h mov [0100h],'1' mov [0101h],'4' mov [0102h],'2' mov [0103h],'8' mov [0104h],'7' mov [0105h],'3' mov [0106h],'5' mov [0107h],'9' mov [0108h],'2' mov [0109h],'6' call PrintScreen mov bx,0100h mov cx,0109h mov dl,2 ; print original array ; first number ; last number ; divider OddNumberLoop mov si, bx mov al,[si] xor ah,ah div dl cmp ah,0 ; check the remainder jnz ContinueOddNumber jmp EvenNumberLoop ContinueOddNumber inc bx cmp bx,cx je Exit jmp OddNumberLoop EvenNumberLoop mov si,cx mov al,[si] xor ah,ah div dl cmp ah,0 jz ContinueEvenNumber jmp Swap ContinueEvenNumber dec cx cmp bx,cx
je Exit jmp EvenNumberLoop Swap push ax mov al,[bx] mov ah,[si] mov [bx],ah mov [si],al pop ax jmp OddNumberLoop Exit call PrintScreen hlt 2016-2017 SPRING TERM PrintScreen proc mov cx, 0 ; number of characters mov ah, 2 ; int 21, ah=2, print char to screen PrintLoop mov si,cx mov dl, [0100h+si] int 21h ; int 21, ah=2, print char to screen inc cx cmp cx,9 ja PrintExit mov dl, ',' int 21h ; int 21, ah=2, print char to screen jmp PrintLoop PrintExit mov dl, 0Dh ; return int 21h mov dl, 0Ah ; new line int 21h ret PrintScreen endp
Q2 The following program code calculates the sum of an array stored in memory. Please run the program step by step and observe its execution..model SMALL.STACK 100H.DATA PROMPT DB 'The Array elements are $' RESULT DB 0DH,0AH,'The Sum of the Array is = $' ARRAY DB 10,20,30,40,50,60,70,80,90,100.CODE MAIN PROC MOV AX, @DATA MOV DS, AX MOV BX, 10 LEA DX, PROMPT MOV AH, 9 MOV CX, BX LEA SI, ARRAY @LOOP XOR AH, AH MOV AL, [SI] CALL OUTDEC INC SI MOV AH, 2 MOV DL, 20H LOOP @LOOP LEA DX, RESULT MOV AH, 9 LEA SI, ARRAY CALL SUM CALL OUTDEC MOV AH, 4CH MAIN ENDP ; initialize DS ; set BX=10 ; load and display the string PROMPT ; set CX=BX ; set SI=offset address of ARRAY ; loop label ; clear AH ; set AX=[SI] ; call the procedure OUTDEC ; set SI=SI+1 ; set output function ; set DL=20H ; print a character ; jump to label @LOOP while CX!=0 ; load and display the string RESULT ; set SI=offset address of ARRAY ; call the procedure SUM ; call the procedure OUTDEC ; return control to DOS
SUM PROC ; this procedure will calculate the sum of an array ; input SI=offset address of the array ; BX=size of the array ; output AX=sum of the array PUSH CX ; push CX onto the STACK PUSH DX ; push DX onto the STACK XOR AX, AX ; clear AX XOR DX, DX ; clear DX MOV CX, BX ; set CX=BX @SUM ; loop label MOV DL, [SI] ; set DL=[SI] ADD AX, DX ; set AX=AX+DX INC SI ; set SI=SI+1 LOOP @SUM ; jump to label @SUM while CX!=0 POP DX ; pop a value from STACK into DX POP CX ; pop a value from STACK into CX ; return control to the calling procedure SUM ENDP ;******************************************************* OUTDEC PROC ; this procedure will display a decimal number ; input AX ; output none PUSH BX ; push BX onto the STACK PUSH CX ; push CX onto the STACK PUSH DX ; push DX onto the STACK CMP AX, 0 ; compare AX with 0 JGE @START ; jump to label @START if AX>=0 PUSH AX ; push AX onto the STACK MOV AH, 2 ; set output function MOV DL, "-" ; set DL='-' ; print the character POP AX ; pop a value from STACK into AX NEG AX ; take 2's complement of AX @START ; jump label XOR CX, CX ; clear CX MOV BX, 10 ; set BX=10 @OUTPUT ; loop label
XOR DX, DX DIV BX PUSH DX INC CX OR AX, AX JNE @OUTPUT MOV AH, 2 @DISPLAY POP DX OR DL, 30H LOOP @DISPLAY POP DX POP CX POP BX OUTDEC ENDP ; clear DX ; divide AX by BX ; push DX onto the STACK ; increment CX ; take OR of Ax with AX ; jump to label @OUTPUT if ZF=0 ; set output function ; loop label ; pop a value from STACK to DX ; convert decimal to ascii code ; print a character ; jump to label @DISPLAY if CX!=0 ; pop a value from STACK into DX ; pop a value from STACK into CX ; pop a value from STACK into BX ; return control to the calling procedure
Q3 Encode the following program code and write your observations about what is the main behavior of this program.
Q4 Write a procedure that exchanges the content of two 16-bit memory block. (Please don t use instruction XCHG). This procedure should have two input parameters which determine the physical addresses of the memory blocks to be swapped. It should be called in main procedure.