0% found this document useful (0 votes)
6 views13 pages

Experiment 4

The document outlines a series of experiments and activities involving ARM assembly functions that perform various bitwise operations (ADD, AND, NOT, OR, XOR) and their integration with a C++ program on a Raspberry Pi Pico board. It includes specific code examples for each operation and instructions for setting up the development environment using Arduino IDE. Additionally, it provides guidance on handling potential upload errors when changing board packages.

Uploaded by

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

Experiment 4

The document outlines a series of experiments and activities involving ARM assembly functions that perform various bitwise operations (ADD, AND, NOT, OR, XOR) and their integration with a C++ program on a Raspberry Pi Pico board. It includes specific code examples for each operation and instructions for setting up the development environment using Arduino IDE. Additionally, it provides guidance on handling potential upload errors when changing board packages.

Uploaded by

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

Experiment 4

4.1​Write an ARM assembly function that takes three constants as inputs


from C program, perform ADD operation, and returns the result to the C
program.
4.2​Write an ARM assembly function that takes two input parameters,
performs an AND logic operation on it, and returns the result to a calling C
program.
4.3​Write an ARM assembly function that takes one input parameter, performs
a bitwise NOT operation on it, and returns the result to a calling C
program.

Objectives:
●​ Learn how to implement bitwise operations in assembly language along with the C++
program.
Components required:
Sl No Description Specification Quantity
1 Raspberry Pi Pico board RP2040 1
2 Micro USB cable 1mtr 1
3 Arduino IDE Software -

Note:

This needs to be built with the board package of MBedOS.


Board and port selection process:
●​ Click on the option shown “Select other board and port …”.
●​ In the search window type “Raspberry”.
●​ Then select the Option appearing below
“Raspberry Pi Pico - Arduino MbedOS RP2040 boards”.
●​ Select a USB port and upload the program.

If you are able to build it using the “Raspberry Pi Pico-Raspberry Pi Pico/RP2040”,


continue and you are free to upload it on to the board.

If you try to upload this program with a different Pico board choices than what is already
running on the board, the board will become unresponsive.

Remember that when you change this package, from what is running on the board, you will
have Upload error and you need to follow the below steps to change the package from one to
the other:
-​ Remove the board from the USB port
-​ Keep pressing the BOOTSEL button while connecting the board to the USB port
of your laptop
-​ In the Arduino IDE, click the Tools menu on the top menu bar, select Board and
port (“UF2 Board”). Upload the program.
-​ Upload the code even if the COM port is not recognized by the Arduino IDE
-​ In case of upload failure, now disconnect the board from the USB port and
connect it back
-​ Now, Arduino IDE should recognize the board and COM port number should
appear and try uploading the software now, it should go through successfully.

Experiment 4.1:

Write an ARM assembly function that takes three constants as inputs from the C program,
performs ADD operation and returns the result to the C program.

#include <stdio.h>
#include <stdlib.h>

extern "C"
{
unsigned int lab_4(unsigned int, unsigned int, unsigned int);
}

void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
unsigned int result;
result = lab_4(10, 20, 30);
myPrint("Result of ASM program: ", result);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}

void myPrint(char *fnName, unsigned int result)


{
Serial.print("The value returned from the assembly fn ");
Serial.print(fnName);
Serial.print(" : ");
Serial.println(result);
}

Asm Program

.global lab_4
.text
lab_4:
add R0,R0,R1
add R0,R0,R2
bx lr
.end

Experiment 4.2:
Write an ARM assembly function that takes two input parameters, performs an AND logic
operation on it, and returns the result to a calling C program.

#include <stdio.h>
#include <stdlib.h>

extern "C"
{
unsigned int lab_4(unsigned int, unsigned int);
}

void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
unsigned int result;
result = lab_4(10, 20);

myPrint("Result of ASM program: ", result);


digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}

void myPrint(char *fnName, unsigned int result)


{
Serial.print("The value returned from the assembly fn ");
Serial.print(fnName);
Serial.print(" : ");
Serial.println(result);
}
Asm Program

.global lab_4
.text
lab_4:
and R0,R0,R1
bx lr
.end

Experiment 4.3:
Write an ARM assembly function that takes one input parameter, performs a bitwise NOT
operation on it, and returns the result to a calling C program.

#include <stdio.h>
#include <stdlib.h>

extern "C"
{
unsigned int lab_4(unsigned int);
}

void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
unsigned int result;
result = lab_4(10);
myPrint("Result of ASM program: ", result);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}

void myPrint(char *fnName, unsigned int result)


{
Serial.print("The value returned from the assembly fn ");
Serial.print(fnName);
Serial.print(" : ");
Serial.println(result);
}

Asm Program

.global lab_4
.text
lab_4:
mvn R0,R0
bx lr
.end

Activity 4.1:
Write an ARM assembly function that takes two input parameters, performs an OR logic
operation on it, and returns the result to a calling C program

#include <stdio.h>
#include <stdlib.h>

extern "C"
{
unsigned int lab_4(unsigned int, unsigned int);
}

void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
unsigned int result;
result = lab_4(10, 20);
myPrint("Result of ASM program: ", result);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}

void myPrint(char *fnName, unsigned int result)


{
Serial.print("The value returned from the assembly fn ");
Serial.print(fnName);
Serial.print(" : ");
Serial.println(result);
}

Asm Program

.global lab_4
.text
lab_4:
orr R0,R0,R1
bx lr
.end

Activity 4.2:
Write an ARM assembly function that takes two input parameters, performs an Exclusive
OR logic operation on it, and returns the result to a calling C program.

#include <stdio.h>
#include <stdlib.h>

extern "C"
{
unsigned int lab_4(unsigned int, unsigned int);
}

void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
unsigned int result;
result = lab_4(10, 20);
myPrint("Result of ASM program: ", result);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}

void myPrint(char *fnName, unsigned int result)


{
Serial.print("The value returned from the assembly fn ");
Serial.print(fnName);
Serial.print(" : ");
Serial.println(result);
}

Asm Program

.global lab_4
.text
lab_4:
eor R0,R0,R1
bx lr
.end

Activity 4.3:
Write an ARM assembly functions for the following expressions.
●​ (val_a >> 4) & val_b
Parameters: val_a, val_b, n=4 ------- (unsigned int)

●​ (val_a << 8) | (~val_b)


Parameters: val_a, val_b, n=8 ------ (unsigned int)

●​ (sVal_a >> 2) & (~sVal_b)


Parameters: sVal_a, sVal_b, n=2 ------ (signed int)
●​ (~sVal_a >> 5) ^ (sVal_b)
Parameters: sVal_a, sVal_b, n=5 ------ (signed int)

i)​ (val_a >> 4) & val_b

#include <stdio.h>
#include <stdlib.h>

extern "C"
{
unsigned int lab_4(unsigned int, unsigned int);
}

void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
unsigned int result;
result = lab_4(10, 20);
myPrint("Result of ASM program: ", result);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}

void myPrint(char *fnName, unsigned int result)


{
Serial.print("The value returned from the assembly fn ");
Serial.print(fnName);
Serial.print(" : ");
Serial.println(result);
}

Asm Program

.global lab_4
.text
lab_4:
mov r2,#4
lsr r0,r0,r2
and r0,r0,r1
bx lr
.end

ii)​ (val_a << 8) | (~val_b)

#include <stdio.h>
#include <stdlib.h>

extern "C"
{
unsigned int lab_4(unsigned int, unsigned int);
}

void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
unsigned int result;
result = lab_4(10, 20);
myPrint("Result of ASM program: ", result);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}

void myPrint(char *fnName, unsigned int result)


{
Serial.print("The value returned from the assembly fn ");
Serial.print(fnName);
Serial.print(" : ");
Serial.println(result);
}

Asm Program

.global lab_4
.text
lab_4:
mov r2,#8
lsl r0,r0,r2
mvn r3,r1
orr r0,r0,r3
bx lr
.end

iii)​ (sVal_a >> 2) & (~sVal_b)

#include <stdio.h>
#include <stdlib.h>

extern "C"
{
unsigned int lab_4(unsigned int, unsigned int);
}
void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
unsigned int result;
result = lab_4(10, 20);
myPrint("Result of ASM program: ", result);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}

void myPrint(char *fnName, unsigned int result)


{
Serial.print("The value returned from the assembly fn ");
Serial.print(fnName);
Serial.print(" : ");
Serial.println(result);
}

Asm Program

.global lab_4
.text
lab_4:
mov r2,#8
asr r0,r0,r2
mvn r3,r1
and r0,r0,r3
bx lr
.end

iv)​ (~sVal_a >> 5) ^ (sVal_b)

#include <stdio.h>
#include <stdlib.h>

extern "C"
{
unsigned int lab_4(unsigned int, unsigned int);
}

void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
unsigned int result;
result = lab_4(10, 20);
myPrint("Result of ASM program: ", result);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}

void myPrint(char *fnName, unsigned int result)


{
Serial.print("The value returned from the assembly fn ");
Serial.print(fnName);
Serial.print(" : ");
Serial.println(result);
}

Asm Program

.global lab_4
.text
lab_4:
mov r2,#5
lsr r0,r0,r2
eor r0,r0,r1
bx lr
.end

You might also like