0% found this document useful (0 votes)
14 views

LAB1

Uploaded by

Stewart Pihelo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

LAB1

Uploaded by

Stewart Pihelo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

DEPARTMENT OF ELECTRICAL, COMPUTER AND TELECOMMUNICATION

ENGINEERING
CTEN 412: MICROCONTROLLERS

LAB 1

By

OTSWELETSE N DITIRELO
17000044
ELECTRICAL AND ELECTRONICS

DR N.DITSHEGO
SUBMISSION DATE: 30-10-2020

1
ABSTRACT
The purpose of this lab was to is to introduce student to the MPLAB environment and also to
convert a C program that performs 8/16-bit unsigned operations into PIC24 assembly language
using the MPLAB X IDE environment .This lab contained 3 tasks where task 1 was to open the
mptst_word project ,assemble it and observe the program memory and the file registers.Task 2
and task 3 was to translate the C program to assembly instructions.All objectives were
successfully met.
OBJECTIVE
The objective of this lab was to;
 Introduce students to the MPLAB environment
 Convert a C program into PIC24 assembly language in MPLAB
PROCEDURE
TASK 1
 Open MPLAB X and the mptst_word project was opened and assembled.
 The program starting at 0x0200 was viewed on the program memory window and the
take screenshot of the program was taken.
 Program’s data on the file registers window was viewed and the screenshot was taken.
TASK 2;myadd.s
 Copy of the mptst_word project was created.
 To do so was done by right clicking on the mptst_word project in the Projects window
and copy was selected ,then myadd was added as the project name.
 Mpts_word.s was open and the file>source file was selected,right clicked on
mptst_word.s and Remove was selectedfrom project.After ,right-clicked on source on
source file and Add existing item was selected and myadd.s was selected.
 The following C program was witten in assembly language.
NB given that BIUST student ID is 17000044
uint16_t u16_lsp;
uint16_t u16_msp;
uint16_t u16_sum;
u16_lsp = 0xY3Y2Y1Y0; // Note that this is a hex value /*0044*/
u16_msp = 0xY7Y6Y5Y4; // Same as above /*1700*/
u16_sum = u16_lsp + u16_msp; /*1700+0044*/

2
TASK 3;mysub
Mpst_word was copied using the procedure given in task 2 with the corresponding assembly
language file named mysub.s.Using the digit of BIUST student ID,an assembly language
program was written that does the following C program .

uint8_t u8_i;

uint8_t u8_j;

uint8_t u8_k;

uint8_t u8_l;

uint8_t u8_m;

u8_i = Y1Y0; /*17*/

u8_j = Y3Y2; /*00*/

u8_k = Y5Y4; /*44*/

u8_l = u8_i + u8_j;


u8_m = u8_k – u8_l;
NB ; GIVEN THAT BIUST STUDENT IS 170044
The code for task 2 and task 3 are found in the results selection below.Also screenshots of the
program memory,watches and file registers were added in the results together with the
verification of the results
RESULTS
TASK 1;Mptst_word project
Mptst_word code
;
; Just check out MPLAB

.include "p24ep128gp202.inc"

.global __reset ;The label for the first line of code.

.bss ;unitialized data section


;;These start at location 0x0800 because 0-0x07FF reserved for SFRs

3
i: .space 2 ;Allocating space (in bytes) to variable.
j: .space 2 ;Allocating space (in bytes) to variable.
k: .space 2 ;Allocating space (in bytes) to variable.

;..............................................................................
;Code Section in Program Memory
;..............................................................................

.text ;Start of Code section


__reset: ; first instruction located at __reset label
mov #__SP_init, w15 ;Initalize the Stack Pointer
mov #__SPLIM_init,W0
mov W0, SPLIM ;Initialize the stack limit register
;__SP_init set by linker to be after allocated data

;User Code starts here.


; C Program equivalent
; #define avalue 20
; uint16 i,j,k;
;
; i = avalue; /* myvalue = 2047 (0x7FF) */
; i = i + 1; /* i++, i = 2048 (0x800) */
; j = i; /* j is 2048 (0x0800) */
; j = j - 1; /* j--, j is 2047 */
; k = j + i; /* k = 4095 (0x0FFF) */
;
.equ avalue, 2047

;i = avalue; /* myvalue = 2047 */

4
mov #avalue, w0 ; w0 = 2047 (w0 is wreg)
mov wreg,i ; i = 2047

; i = i + 1;
inc i ;i=i+1

;j=i
mov i,wreg ; w0 = i
mov wreg,j ; j = w0

; j = j - 1; /* j--, j is 100 */
dec j ; j= j - 1

;k=j+i
mov i,wreg ; w0 = i
add j,wreg ; w0 = w0+j
mov wreg,k ;k=0

done:
goto done ;Place holder for last line of executed code

.end ;End of program code in this file


View from program memory window starting at 0x0200

5
View of a file register window of program data at 0x1000

The added watch window is shown below with the type,address and the value

The following is showing the data memory window contents.

Given decimal number 2048


HEXA-DECIMAL
0800
IN BINARY
0000 1000 0000 0000
DECIMAL 2047
HEXDECIMAL
07FF

6
BINARY
0000 0111 1111 1111
ADDITION OF TWO NUMBERS
DECIMAL HEXA-DECIMAL BINARY
2048 0800 0000 1000 0000 0000
+2047 + 07FF + 0000 0111 1111 1111
4095 0FFF 0000 1111 1111 1111
The calculation above show the expected results hence it matches with the results from the file
registers.
TASK 2;myadd.s
CODE
;
; Just check out MPLAB

.include "p24ep128gp202.inc"

.global __reset ;The label for the first line of code.

.bss ;unitialized data section


;;These start at location 0x0800 because 0-0x07FF reserved for SFRs
lsp: .space 2 ;Allocating space (in bytes) to variable.
msp: .space 2 ;Allocating space (in bytes) to variable.
sum: .space 2 ;Allocating space (in bytes) to variable.

;..............................................................................
;Code Section in Program Memory
;..............................................................................

.text ;Start of Code section


__reset: ; first instruction located at __reset label

7
mov #__SP_init, w15 ;Initalize the Stack Pointer
mov #__SPLIM_init,W0
mov W0, SPLIM ;Initialize the stack limit register
;__SP_init set by linker to be after allocated data

;User Code starts here.


; C Program equivalent
; #define avalue 2047
; uint16 lsp,msp,sum;

;uint16_t u16_lsp;
;uint16_t u16_msp;
;uint16_t u16_sum;
;u16_lsp = 0xY3Y2Y1Y0; // Note that this is a hex value /*lsp=0x1700 (5888 decimal)*/
;u16_msp = 0xY7Y6Y5Y4; // Same as above /*msp=0x0044 (68 decimal)*/
;u16_sum = u16_lsp + u16_msp; /*sum=0x1744 (5956 decimal)*/

mov #0x1700, w0 ; w0 = 1700 (w0 is wreg)


mov wreg,lsp ; lsp = 1700

mov #0x0044,w0 ; w0 = msp


mov wreg,msp ; msp = w0

; sum= lsp + msp


mov lsp,wreg ; w0 = lsp
add msp,wreg ; w0 = w0+msp
mov wreg,sum ; sum = 1744

8
done:
goto done ;Place holder for last line of executed code

.end ;End of program code in this file

View from program memory

View from file register

View from watches

9
Given the lsp value as 1700 and msp as 0044 in hexadecimal
1700 0044
IN DECIMAL IN DECIMAL
5888 68
IN BINARY IN BINARY
0001 0111 0000 0000 0000 0000 0100 0100
ADDITION OF NUMBERS
IN HEXA-DECIMAL IN DECIMAL IN BINARY
1700 5888 0001 0111 0000 0000
+0044 +0068 +0000 0000 0100 0100
1700 5956 0001 0111 0100 0100
The calculated results correspond with the values found in watches and in the file registers.
TASK 3;mysub
Mysub.s code

;
; Just check out MPLAB

.include "P24EP128GP202.inc"

.global __reset ;The label for the first line of code.

.bss ;unitialized data section


;;These start at location 0x0800 because 0-0x07FF reserved for SFRs
u8_i: .space 1 ;Allocating space (in bytes) to variable.
u8_j: .space 1 ;Allocating space (in bytes) to variable.
u8_k: .space 1 ;Allocating space (in bytes) to variable.
u8_l: .space 1 ;Allocating space (in bytes) to variable.
u8_m: .space 1 ;Allocating space (in bytes) to variable.

10
;..............................................................................
;Code Section in Program Memory
;..............................................................................

.text ;Start of Code section


__reset: ; first instruction located at __reset label
mov #__SP_init, w15 ;Initalize the Stack Pointer
mov #__SPLIM_init,W0
mov W0, SPLIM ;Initialize the stack limit register
;__SP_init set by linker to be after allocated data

;User Code starts here.


; C Program equivalent
; uint8 i,j,k,l,m;
;
; u8_i = 17; /* i = 17 (0x11) */
; u8_j = 00; /* j is = 00 (0x00) */
; u8_k = 44; /* k is = (0x2C) */
; u8_l = u8_i + u8_j; /* l is = 17 (0x11) */
; u8_m = u8_k - u8_l; /* k = 27 (0x1B) */
;
.equ avalue, 17
.equ bvalue, 00
.equ cvalue, 44

;u8_i = avalue;
mov #0x0000, w0 ;initialising w0 to 0x0000.
mov.b #avalue, w0 ; w0 = 17 (w0 is wreg)
mov.b wreg,u8_i ; u8_i = 17

11
; u8_j = bvalue;
mov.b #bvalue, w0 ;w0 = 00
mov.b wreg,u8_j ;u8_j = 00

; u8_k = cvalue;
mov.b #cvalue, w0 ; w0=44
mov.b wreg,u8_k ; u8_k = 44

; u8_l = u8_i + u8_j;


mov.b u8_i,wreg ; wreg = u8_i (17)
add.b u8_j,wreg ; wreg = u8_i + u8_j (17+00)
mov.b wreg,u8_l ; u8_l = wreg (17)

; u8_m = u8_k -u8_l;


mov.b u8_l,wreg ; wreg = u8_l (17)
Sub.b u8_k,wreg ; wreg = u8_k - u8_l (44-17)
mov.b wreg,u8_m ; u8_m = wreg (27)
done:
goto done ;Place holder for last line of executed code

.end ;End of program code in this file


View from watches

12
View from file registers

u8_i u8_j u8_k u8_l u8_m


decimal 17 00 44 17 27
hexadecimal 11 00 2C 11 1B
binary 10001 00000 101100 10001 11011

u8_l =u8_i+u8_j
DECIMAL HEXA-DECIMAL BINARY
17 11 10001
+00 +00 +10001
17 11 100010
u8_m=u8_k-u8_l
DECIMAL HEXA-DECIMAL BINARY
44 2C 101100
-17 -11 -100010
27 1B 011011
From the Task the challenge was into subtracting binary numbers.Also values of u8_k and u8_l
didn’t appear in the file register but they did appeared in the watches window,but the calculated
answers correspond to the file register values.

DISCUSSION

13
It is quite a challenge to differentiate between data address and the actual data. Having to move
the value from data address where it is stored to a temporary storage for the case where watches
were used for the purpose of arithmetic. Then move the result to the address to where it is stored,
it is quite an experience and a good way to learn the language and its operation.
CONCLUSION
The Objective of this lab were clearly met hence the lab was successful,the intern was to
introduce the environment of MPLAB X and aslo to learn how to convert C program to the
assembly laungage . Doing some simple programming to get familiar with the coding of the
environment in which it is assembly language. The two exercises given for adding and
subtracting hexa-decimal numbers is quite a good example for introduction of assembly
language.

14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy