UNIVERSIDAD RICARDO PALMA
INGENIERÍA INGENIERÍA MECATRÓNICA
PRACTICA N° 1
MICROPROCESADORES MICROPROCESADORES Y MICROCONTROLADORES MICROCONTROLADORES
EXERCISE PART 1:
The objective of this experiment is to familiarize the students with microprocessor and loading some simple programs.
Write following codes and perform indicated operations. Take help from previously stated operations for loading and executing the program.
MOV Command:
a) MOV AX,3012
MOV is a basic command to assign a number to a variable. For example following word can be converted to a MOV command as, x = 33; MOV AX,33d (d stays for decimal) here AX stands for x. In this way we can declare variables. MOV can also be used to assign another registers value. x = 33 y=x
Observe content of AX register. MOV BX,AX Observe content of BX register. What operation happened here? b) MOV AX,30 AX,30h h; MOV [2010],AX; MOV BX,[2010] Observe content of BX register. What operation happened here?
MOV AX,33 MOV BX,AX
c) MOV SI,1256h
MOV can also be used to assign some value to a memory location or from a memory location. MOV AX,[1000H] Storing value to AX from memory location 1000H MOV [1000H],AX Storing value to memory location 1000H from AX MOV [BX],AX Storing value of memory location that has address given by the value of BX, to AX More about MOV:
MOV command must be used with registers of equal size. MOV AX,BL is not correct as AX is of 16bits and BL is of 8 bits. Similarly MOV BL,1652h is not correct as 1652h is 16 bit number and available space for BL is only 8 bits. Remember if you write ‘h’ after a number then it is hexadecimal number. If nothing is written then default type is decimal. NEG command: NEG command
is
used
to
complement of any number. MOV AL,30h NEG AL
Now AL will have the value D0h DOCENTE: Ing. Luis Pacheco Cribillero
get
the
2’s
MOV [SI],3251h MOV AX,[SI] Observe content of BX register. What operation happened here? d) MOV AL,87h
NEG AL NEG AL Write value of AL for every step. ARITHMETIC COMMANDS: ADD, SUB, DIV, MUL are
all arithmetic commands. ADD is used to add two numbers. For example following lines can be converted to, x = 1236H y = 1438H z = x+y MOV AX, 1236H MOV BX, 1438H ADD AX,BX
This command adds AX and BX content and stores it to AX register. Similarly SUB command is used for subtraction of two numbers. For example, x = 1236H y = 1438H z = x – y MOV AX, 1236H MOV BX, 1438H SUB AX,BX
AX holds the subtraction result. MICROPROCESADORES MICROPROCESADORES
2012 – I
1
UNIVERSIDAD RICARDO PALMA
INGENIERÍA MECATRÓNICA
MUL command is used to multiply two operands. x = 146H y = 5898H z = x*y MOV AX,1456H MOV BX,5898H MUL BX
AX holds the lower 16 bit and DX holds the upper 16 bit result. For example result of upper operation is 0709A310. So AX holds A310 and DX holds 0709. DIV is used to perform division operation. x = 5327H y = 15F2H z = x/y MOV AX,5327H MOV BX,15F2H DIV BX
AX holds the result and DX holds the remainder. For example for upper operation AX holds 3H and DX holds 1151H.
MICROPROCESADORES Y MICROCONTROLADORES
x = 30D y = 50D z=x+y w = z*y x = w – 20D y = x/5 u = x%5 Clearly indicate which register contains which value clearly. 2. Write a assembly code that will put 1265H to memory location 2100H and 4512H in memory location 1287H. Access these values via registers and perform subtraction operation. 3. Perform Multiplication of 1254H and 4512H. Store the higher 16 bit of the result to CX result. 4. Perform Division operation of 4512H by 1254H. ADD 03H with remainder value.
PRACTICA N° 2 Objective: • Using logical
instructions
in
assembly
language. EXERCISE PART 2:
• Incorporating Jump commands in assembly
Write following codes and perform indicated operations.
programs.
a) MOV AX,1782H
with logical and Jump instructions.
MOV BX,1278H ADD AX,BX Examine register contents
Introduction:
Both logical instructions and Jump commands are widely used in assembly language. These commands are explained below.
b) MOV AX,1782H
MOV BX,1278H SUB AX,BX Examine register contents
Logical instructions:
Logical instructions include NOT, AND, OR, XOR, TEST etc. instructions. There job is to compare the data values and make results according to logic specified. For example,
c) MOV AX,2782H
MOV BX,1278H MUL BX Examine register contents
MOV BX, 30H ; In binary 110000 NOT BX ; In binary 001111
This code takes BX value and then complements all the bits and stores the new value to BX. So it stores 0F value in BX after executing NOT operation. For another example,
d) MOV AX,57F2H
MOV BX,1375H DIV BX Examine register contents Home Task:
1. Write complete assembly language for the following operation and verify it in DEBUG software. DOCENTE: Ing. Luis Pacheco Cribillero
• Writing simple assembly language programs
MOV BX, 70H ; In binary 1110000 MOV CX, 40H ; In binary 1000000 AND CX, BX ; In binary 1000000
MICROPROCESADORES
2012 – I
2
UNIVERSIDAD RICARDO PALMA
INGENIERÍA MECATRÓNICA
operation performs bit by bit AND operation and then stores the value in first operand. In upper code CX holds the final result. AND
MOV BX, 70H ; In binary 1110000 MOV CX, 40H ; In binary 1000000 OR CX, BX ; In binary 1110000 OR operation performs bit by bit OR
MICROPROCESADORES Y MICROCONTROLADORES
AND CX, BX HLT CODE ENDS END Observe content of CX register. What operation happened here? (b) Program 2:
operation and then stores the value in first operand. In upper code CX holds the final result. Similar case happens for XOR and it is given below, MOV BX, 70H ; In binary 1110000 MOV CX, 40H ; In binary 1000000 XOR CX, BX ; In binary 0110000 Test operation is a little different
from AND operation. It performs bit by bit AND operation but it does not change any operands value.
CODE SEGMENT ASSUME CS:CODE, DS:CODE MOV BX, 3256H MOV CX, 1554H XOR CX, BX HLT CODE ENDS END Observe content of CX register. What operation happened here? (c) Program 3:
CODE SEGMENT ASSUME CS:CODE, DS:CODE MOV AX, 1027H All the logical instructions stated above MOV BX, 5A27H upgrades all the flag register values except AF MOV CX, 54A5H register. NOT command does not effect any OR AX, BX flags. How flags are affected is stated below. XOR AX, CX MOV BX, 70H ; In binary 1110000 NOT AX MOV CX, 40H ; In binary 1000000 TEST CX, BX AND CX, BX ; In binary 1110000 AND CX, AX After this operation Zero Flag is 0 (ZF = 0; as HLT the value of CX is not 0), Carry Flag is 0 (CF = CODE ENDS 0; as there is no carry), Parity Flag is 0 (PF = 0; END as there are odd number of 1’s), Sign Flag is 0 Perform this operation in single step mode and (SF = 1), Overflow Flag is 0 (OF = 0; as there is write the values of registers for every step. no overflow). In this all the flags can be Obtain binary values for upper hexadecimal determined. values and perform bit by bit operation for every Do not confuse yourself with semicolon given step. Compare your hand calculation with after each line in assembly codes above. obtained result. MOV BX, 70H ; In binary 1110000 MOV CX, 40H ; In binary 1000000 TEST CX, BX ; In binary CX value is 1000000
Comments are written after semi colon ‘;’ in
assembly language.
JUMP Commands:
Exercise Part 1:
Sometimes it is necessary to go from one line of program to another line without executing some intermediate lines. For this Jump commands are used. We can explain this with a simple example. MOV AX, 3254H MOV BX, 1F4BH MOV CX, 412AH
Write following codes and perform indicated operations. (a) Program 1:
CODE SEGMENT ASSUME CS:CODE, DS:CODE MOV BX, 3256H MOV CX, 1554H DOCENTE: Ing. Luis Pacheco Cribillero
MICROPROCESADORES
2012 – I
3
UNIVERSIDAD RICARDO PALMA
INGENIERÍA MECATRÓNICA
ADD AX, CX JMP L3T2 SUB AX, BX L3T2: AND AX, BX HLT In this example L3T2 is a level. As we can see in fifth line JMP command is used. It makes the program to go from fifth line to L3T2 level that is seventh line. So sixth line is not executed. There are two types of Jump commands. These are (i) Conditional jump and (ii) Unconditional Jump. Previous example is an unconditional jump. Conditional Jumps are like if statements. If some flags are affected only then these jump instructions executed. We can look at the following example, MOV AX, 125BH MOV BX, 125BH MOV CX, 412AH SUB AX, BX JZ L3T2 DIV BX L3T2: AND AX,CX HLT Clearly observe the code. In fourth line subtraction operation is performed. As both AX and BX have same value. Their subtracted value is 0. So ZF is set to 1. In fifth line JZ L3T2 is written. It means if ZF = 1 then go to L3T2:. Otherwise continue. As ZF = 1, program moves to eighth line. This is a conditional Jump. Some other conditional jumps are, Command
Condition of Jump
JA/JNBE JBE/JNA JNB/JAE/JNC JB/JNAE/JC JG/JNLE JLE/JNG JGE/JNL JL/JNGE JZ/JE JNZ/JNE JS JNS JPE/JP
CF =0, ZF = 0 CF = 0 or ZF = 0 CF = 0 CF = 1 SF OF = 0, ZF = 0 SF OF = 0, ZF = 1 SF OF = 0 SF OF = 1 ZF = 1 ZF = 0 SF = 1 SF = 0 PF = 1
DOCENTE: Ing. Luis Pacheco Cribillero
MICROPROCESADORES Y MICROCONTROLADORES
JPO/JNP JO JNO JCXZ
PF = 0 OF = 1 OF = 0 CX = 0
Exercise Part 2:
Write following codes and perform indicated operations. (a) Program 1:
CODE SEGMENT ASSUME CS:CODE, DS:CODE MOV AX, 7A24H MOV BX, 15A3H SUB AX, BX JMP L3T2 EEE316: DIV BX JMP Last L3T2: MOV CX, 45B1H AND AX, CX TEST AX, BX JMP EEE316 Last: HLT CODE ENDS END Perform this operation in single step mode and write the values of registers for every step. Explain why we need ‘Last’ termed level? What
will happen if it is not used? (b) Program 1:
CODE SEGMENT ASSUME CS:CODE, DS:CODE MOV AX, 7A24H MOV BX, 95A3H ADD AX, BX JC L3T2 EEE316: OR AX, 23H JNZ Last L3T2: MOV CX, 0FC7H SUB AX,CX JZ EEE316 Last: HLT CODE ENDS END Update the register values in every step. Home Task: 1. Write an assembly code that will determine whether a number is greater than 5 or equal of
MICROPROCESADORES
2012 – I
4
UNIVERSIDAD RICARDO PALMA
INGENIERÍA MECATRÓNICA
less, and put 0 or 1 or 2 for the conditions in DX. 2. Subtract 86B1H from 3F42H and store 0 in CX if overflow occurs and 1 if no overflow occurs. 3. Take 2 arbitrary numbers x and y. If x>1000H perform x+y. If y<1000H perform x-y. If
MICROPROCESADORES Y MICROCONTROLADORES
x>1000H and y<100H perform x = x’.
PRACTICA N° 3 Objective:
To get familiar with Rotate and Shift commands in assembly language. To use loops in complex problems. Introduction: Shift and Rotate command:
Shift and Rotate commands are used to convert a number to another form where some bits are shifted or rotated. Basic difference between shift and rotate is shift command makes “fall of” bits at
the end of register. Where rotate command makes “Wrap around” at the end of the register.
There are both arithmatic (SAL and SAR) and logical (SHL and SHR) shift instructions. Graphical operation for these commands are shown below.
Some simple codes can be given to clarify the idea. MOV CL,03H ; MOV AX,02F3H ; In binary 0000 0010 1111 0011 SHR AX,CL ;In binary 0000 0000 0101 1110 In this procedure, SHR commands inserts 0’s
from right side. Each time a 0 is inserted left most bit is vanished from register content. MOV CL,03H ; MOV AX,82F3H ; In binary 1000 0010 1111 0011 SAR AX,CL ; In binary 1111 0000 0101 1110
In this procedure, SHR commands inserts MSB content from right side. Each time it is inserted left most bit is vanished from register content. MOV CL,03H ; MOV AX,82F3H ; In binary 1000 0010 1111 0011 ROR AX,CL ; In binary 0111 0000 0101 1110
The whole procedure can be visualized as follows.
Here rotate by 3 operation is shown. It is clearly seen that every bit is assigned to a new position that is 3 places away from previous one. Unlike the shift command no right bit is destroyed. It is placed in the leftmost position. Exercise part 1:
(a) Program 1: CODE SEGMENT ASSUME CS:CODE MOV CL,02H MOV AX,105AH DOCENTE: Ing. Luis Pacheco Cribillero
MICROPROCESADORES
2012 – I
5
UNIVERSIDAD RICARDO PALMA
INGENIERÍA MECATRÓNICA
SHL AX,CL HLT CODE ENDS END Obtain AX register value in write the previous value and present value in binary form. What type of operation is this? (b) Program 2: CODE SEGMENT ASSUME CS:CODE MOV CL,04H MOV AX,564AH SAL AX,CL HLT CODE ENDS END Obtain AX register value in write the previous value and present value in binary form. What type of operation is this? (c) Perform for similar values of AX and CL with ROL, ROR. RCL, RCR command. LOOP in assembly language:
Loop commands are used to perform same operation again and again. This is like for, while type instructions in ‘C’ or ‘MATLAB’. A common
example can be shown as, MOV CX,0100D MOV AX,564AH Lev: DEC AX Loop LEV HLT Here CX acts as a count register. Loop Lev instruction leads instruction to go back to Lev level until CX is zero. Each time Lev level is executed CX is decreased by 1. Loop command can be used for waiting purposes. Such as, MOV CX,0100D Wt: NOP Loop Wt HLT Here the loop is executed until CX is zero. If 1 loop takes 1ms, the program will wait for 100ms. DOCENTE: Ing. Luis Pacheco Cribillero
MICROPROCESADORES Y MICROCONTROLADORES
Exercise part 2:
(a) Program 1: CODE SEGMENT ASSUME CS:CODE MOV AX,1025H MOV BX,475AH MOV CX,50H Lev: INC AX DEC BX LOOP Lev HLT CODE ENDS END Observe the operation of this code. What happens when the loop is executed again and again. (b) Program 2: This code is to find GCD (Greatest Common Divisor)
CODE SEGMENT ASSUME CS:CODE MOV AX,5H MOV BX,3H Lev: XOR DX,DX DIV BX MOV AX,BX MOV BX,DX TEST DX,0H JNZ Lev HLT CODE ENDS END Here GCD of 5 and 3 are found. You can change the values of AX and BX and obtain the result for any other values. Find GCD of 08D4H and 235H. Result: (c) Find Least Common Multiplier of 12H and 25H. Report:
1. Suppose x = 20 and y = 28. Add y with x for 30 times. 2. Multiply 12 by 6 until result is below 3000H. If result is greater than this, divide the result by 2 for 3 times. 3. You can get input into microprocessor via following code. MOV AH, 1H ; keyboard input subprogram
MICROPROCESADORES
2012 – I
6
UNIVERSIDAD RICARDO PALMA
INGENIERÍA MECATRÓNICA
INT 21H HLT Take input from the keyboard until b is pressed. PRACTICA N° 4 Objective: • To be familiar with stack operations. • Calling and executing functions
within
assembly language programs. • Arrays in assembly language programming. Introduction to stack:
Stack is a segment where some register values can be stored so that it is not lost. In assembly language programming we have only four registers with which we can perform operations. These are AX, BX, CX and DX. But in many problems we may need a lot of registers. So we need to reuse these registers again and again. This can be done by storing present value of AX in stack segment. Now we can perform other tasks with AX. This will certainly change content of AX. After this getting the value of AX from stack and restoring its previous value. The first job (storing value of AX in stack) is done by
MICROPROCESADORES Y MICROCONTROLADORES
memory that has address 2 bytes lower than previous one. All these can be shown as, 1. PUSH AX 2. PUSH BX 3. PUSH CX 4. … 5. … 6. …
7. POP CX 8. POP BX 9. POP AX Here the numbers are simply program line numbers. In line 1 AX is PUSHed. Now suppose stack starts with address value 100H. This is called Stack Pointer (SP this indicates where to put a value if something is PUSHed). Now AX’s value is stored in 100H address. Stack pointer is changed to 0FEH. This means it is decremented by 2. If BX is PUSHed then its value is stored in 0FEH and new value of SP is 0FCH. Finally if CX is PUSHed its value is stored in 0FCH. All these are shown graphically below. At the beginning.
PUSH command.
Second job (restoring value of AX from stack) is done by POP command in assembly language. A simple program can clarify this. MOV AX, 3256H; Stores 3256H in AX. This is AX’s original value PUSH AX; Puts AX’s value in stack MOV AX, 1254H; AX is assigned to an intermediate value 1254H DEC AX; New value of AX is 1253H. Certainly
After the call, PUSH AX
After the call, PUSH BX
AX’s value is changed. POP AX; This restores AX’s previous value and
now AX is 3256H Now how stack segment is organized. Stack segment is a different segment other than default segment 0000 (in which we use to work). It is an array of memory that can store values. Obviously every memory content has an address. Each time a PUSH instruction is called, value of corresponding register is stored in stack memory. If another PUSH is called that corresponding registers value is stored in a DOCENTE: Ing. Luis Pacheco Cribillero
After the call, PUSH CX
When POP command is executed the value stored last in stack, is freed first. Here POP CX has to be called before BX or AX can be
MICROPROCESADORES
2012 – I
7
UNIVERSIDAD RICARDO PALMA
INGENIERÍA MECATRÓNICA
POPed. POP command automatically increases SP value by 2. So total operation of POP is opposite to PUSH. After the call, POP CX After the POP BX
After the call, POP AX
call,
MICROPROCESADORES Y MICROCONTROLADORES
operation. Clearly observe the program and state what happens in every line. CODE SEGMENT ASSUME CS:CODE, DS:CODE MOV AX, 42A6H MOV BX, 2E5AH MOV CX, 12H PUSH AX PUSH BX PUSH CX LEV: ADD AX,BX LOOP LEV POP CX POP BX POP AX HLT CODE ENDS END Introduction to CALL and RET: Like ‘C’ or ‘MATLAB’, in assembly language we
can create functions that can be accessed from main program. CALL instruction is needed to access a function. Each function is ended with a RET to go back to main program. Suppose Exercise Part 1: Shatadru is the name of a procedure (in (a) Program 1: This program stores two values assembly language functions are called in AX and BX. It finds remainder of division procedures). It is called from the main program. operation between AX and BX, and stores it to What happens is stated step by step below, BX. AX’s value need not to be altered. Clearly At the instant of procedure calling, observe all the steps and write what happens. CODE SEGMENT ASSUME CS:CODE, DS:CODE MOV AX, 3256H MOV BX, 125AH PUSH AX LEV: SUB AX, BX CMP AX, BX JG LEV MOV BX, AX POP AX HLT CODE ENDS END (b) Program 2: Here BX is added with AX for CX times. It is desired that values of AX, BX, CX will be regained after the complete DOCENTE: Ing. Luis Pacheco Cribillero
MICROPROCESADORES
2012 – I
8
UNIVERSIDAD RICARDO PALMA
INGENIERÍA MECATRÓNICA
MICROPROCESADORES Y MICROCONTROLADORES
When SHATADRU is called next instruction When SHATADRU is finishing IP is set to address 0102H is saved in stack. And IP is 0260H transferred to 0200H. Remember IP is the address of the code that is executing.
At the instant of procedure calling, When SHATADRU is called next instruction address 0102H is saved in stack. IP is transferred to 0202H.
At the time back in the main program,
When SHATADRU is finished IP is set to 0102H from stack. ,
At the instant of procedure finishing DOCENTE: Ing. Luis Pacheco Cribillero
MICROPROCESADORES
2012 – I
9
UNIVERSIDAD RICARDO PALMA
INGENIERÍA MECATRÓNICA
MICROPROCESADORES Y MICROCONTROLADORES
Exercise part 2: (a)Multiplication program:
Here multiplication is done by shifting and then adding a numbers. This is done as follows. 1101 X1001 1101 0000X 0000XX 1101XXX 1110101 CODE SEGMENT ASSUME CS:CODE, DS:CODE MAIN PROC MOV AX, 123H MOV BX, 16H CALL MULTIPLY MULTIPLY PROC PUSH AX PUSH BX XOR DX, DX
LEV:
REPEAT: TEST BX, 1 JZ END_IF ADD DX, AX END_IF: SHL AX, 1 SHR BX, 1 JNZ REPEAT POP BX POP AX RET MULTIPLY ENDP END MAIN CODE ENDS END (b)Program 2: This calculates GCD of three or more numbers. It uses a procedure that calculated GCD of two numbers, as discussed in the previous experiment. CODE SEGMENT ASSUME CS:CODE, DS:CODE MAIN PROC MOV AX,9H MOV BX,5H MOV CX,4H DOCENTE: Ing. Luis Pacheco Cribillero
Introduction language:
MOV DX,3H CAll GCD MOV BX,CX XCHG AX,BX CAll GCD MOV BX,DX XCHG AX,BX CAll GCD HLT GCD PROC PUSH CX PUSH DX MOV DX,0H SUB AX,BX CMP AX,BX JG LEV MOV CX,AX MOV AX,BX MOV BX,CX CMP BX,DX JNE LEV POP CX POP DX RET GCD ENDP END MAIN CODE ENDS END to
Arrays
in
assembly
Array is nothing but series of data stored in successive locations in memory. It can be showed graphically as,
Simple command to insert an array in assembly language is, W DW 10,20,30,40,50,60 Or MSG DB ‘abcde’
MICROPROCESADORES
2012 – I
10
UNIVERSIDAD RICARDO PALMA
INGENIERÍA MECATRÓNICA
Here DW is data word or word array, DB is data byte or byte array. W and MSG are array names. [W] indicates the starting address. DUP command is used to create an array of same numbers. For example, ALPHA DW 100 DUP(0) Creates an array containing 100 0s. DUP can also be nested. GAMMA DB 5,4, 3 DUP (2, 3 DUP (0), 1) Creates an array of 5,4,2,0,0,0,1,2,0,0,0,1,2,0,0,0,1 Memory access for array is simple. Suppose we have W, an array of 50 elements. We want to
MICROPROCESADORES Y MICROCONTROLADORES
swap its 25’th value with 31’st value. Now W
indicates first element of array. W+2 second, W+4 third. So W+48 25’th and W+60 31’st. So
code will be like this. MOV AX, W+48 XCHG W+60, AX MOV W+48, AX Report:
1. Find Least Common Multiplier of four numbers. 2. Perform division operation by shifting. 3. Take an array and fine its mean value.
DOCENTE: Ing. Luis Pacheco Cribillero
MICROPROCESADORES
2012 – I
11