Experiment 4
Experiment 4
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:
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);
}
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);
.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);
}
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);
}
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);
}
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)
#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);
}
Asm Program
.global lab_4
.text
lab_4:
mov r2,#4
lsr r0,r0,r2
and r0,r0,r1
bx lr
.end
#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);
}
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
#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);
}
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
#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);
}
Asm Program
.global lab_4
.text
lab_4:
mov r2,#5
lsr r0,r0,r2
eor r0,r0,r1
bx lr
.end