Lab 02
Lab 02
Contents
1 Acknowledgements 2
2 Administrivia 2
2.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Deliverable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3 Hardware Resources 2
4 Introduction 3
4.1 Assembler Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4.2 Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4.3 Data Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4.4 Memory Addressing Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4.5 Status Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4.6 Important Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
5 Lab Tasks 5
5.1 Data Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
5.1.1 Task A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
5.2 Status Register, Arithmetic & Logical operations . . . . . . . . . . . . . . . 5
5.2.1 Task B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
5.2.2 Task C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
5.2.3 Task D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5.3 Memory Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5.3.1 Task E . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1
1 Acknowledgements
This lab exercise is prepared by Mohammad Azfar Tariq and Muhammad Usman under the
supervision of Dr. Rehan Ahmed for the course EE-222 Microprocessor Systems. Reporting
any error or discrepancy found in the text is appreciated.
2 Administrivia
2.1 Objectives
By the end of this lab you will be able to;
2.2 Deliverable
You are required to submit a report including;
3 Hardware Resources
No hardware implementation or resources are required for this lab.
2
4 Introduction
4.1 Assembler Directives
The assembler needs some directions to assemble the code in a proper way as desired by the
programmer. These directions are given through assembler directives. They are not part of
the code.
Directive Description Example
.EQU Define a symbol/name with a fixed value .EQU Time=33
.CSEG Instructions written after this directive are .CSEG
placed in code memory. If not declared ex- ;your code
plicitly, everything is placed in code memory
by default
.ORG Place the upcoming instructions at the mem- .ORG 0x0F
ory address declared
We expect you to use assembler directives in your codes for lab exercises.
4.2 Labels
Labels are used to point the instructions or data written after it. Compiler replaces them
by respective address of instruction after compilation.
1. Register File
3. SRAM
4. EEPROM
3
4.5 Status Register
Status Register (SREG) is a regular 8 bit register with each bit representing crucial indications
about operations being performed in the Arithmetic Logic Unit (ALU).
7 6 5 4 3 2 1 0
I T H S V N Z C
For the moment, ignore I & T while a thorough analysis of the other flag bits is intended in
this lab.
1
https://www.microchip.com/webdoc/avrassembler/avrassembler.wbi nstructionl ist.html
4
5 Lab Tasks
5.1 Data Space
The address ranges of sections in data space are critical for implementation of a system
involving Microcontrollers.
5.1.1 Task A
Consult the Datasheet2 for ATmega16A and jot down the initial and final addresses of each
address space in your report.
Example: Address Range of Register File begin from 0x000 and ends at 0x01F.
5.2.2 Task C
Multiply 0x011 and 0x0AB
1. Considering both of them unsigned: mul.
2. Considering both of them signed: muls.
3. Considering 0x011 signed and 0x0AB unsigned: mulsu.
Record and analyze the output in the table4 (or doc5 ).
2
http://ww1.microchip.com/downloads/en/devicedoc/atmel-8154-8-bit-avr-atmega16ad atasheet.pdf
3
https://github.com/Uthmanhere/EE222/blob/master/02 taskA.docx
4
https://github.com/Uthmanhere/EE222/blob/master/02 taskE.pdf
5
https://github.com/Uthmanhere/EE222/blob/master/02 taskE.docx
5
5.2.3 Task D
In digital systems, packed binary coded decimal (BCD) is a type of binary code in which
each decimal digit is represented by four bits. A small algorithm called “Double Dable”6 is
used to convert binary numbers to BCD representation. Binary numbers from 0 − 99 will
have a BCD representation in 8-bits. Follow the algorithm explained below and write an
assembly program that converts binary numbers (0 − 99) to 8-bit BCD. Keep the result in
R20.
The lower 4-bits of 8-bit BCD represents the “Unit digit” and upper 4-bits represents “Tens
digit”.
1. Clear 8-bit BCD to zero.
2. Check (separately) if “Unit digit” and “Tens digit” is less than or equal to four (1002 ).
3. If not, add three (112 ) to them (separately).
4. Shift MSB of binary number to LSB of BCD representation and repeat from step 2
until all the bits of binary number are shifted to BCD.
5. Stop after last shift.
For example consider the conversion of 99 (011000112 ),
BCD
Tens Unit Binary Number
0000 0000 01100011
0000 0000 11000110 Shift left one bit
0000 0001 10001100 Shift left one bit
0000 0011 00011000 Shift left one bit
0000 0110 00110000 Shift left one bit
0000 1001 00110000 Add 3 to Unit because greater than 4
0001 0010 01100000 Shift left one bit
0010 0100 11000000 Shift left one bit
0100 1001 10000000 Shift left one bit
0100 1100 10000000 Add 3 to Unit because greater than 4
1001 1001 00000000 Shift left one bit
9 9
Complete the code below such that it works for any value of “Num” between 0 − 99.
1 .EQU Num=23
2 .ORG 0 x00
3 LDI R20 , Num
4 ;− − − − − Your code here− − − − −
5
6 ;− − − − − − − − − − − − − − − − −
7 End :
8 RJMP End
6
https://en.wikipedia.org/wiki/Double dabble
6
5.3 Memory Operations
5.3.1 Task E
When we program AVR microcontrollers in C, the “int” data type is of 2-bytes. But as you
have seen till now, in assembly, all the instructions handle data in form of a single byte and
the data chunks greater than a byte are first broken down into bytes and then processed
separately. Suppose we have two integers 128410 and 77510 . Write an assembly program
that;
• Step 3: Subtracts them (1284 - 775) and keep result in R22 and R23.