Write a C program that performs 8 bit signed and unsigned integer addition and s
ID: 3802901 • Letter: W
Question
Write a C program that performs 8 bit signed and unsigned integer addition and subtraction. For this, you will create a structure that represents the Program Status Register, with flags N,Z,V and C (can be bool or int) and a test function (inside main) that tests (displays result and flag status) all the signed and unsigned addition and subtraction.
Details: Your C program will have the following structure: //Header file #include stdint.h> //For creating 8-bit integer. By default it is 32 //Structure struct flagf //Lists all the flag lstatus; //creating global structure i is advised //Functions prototype for arithmetic operation //signed addition //signed subtraction //Unsigned addition //Unsigned subtraction /Main function //calls the above prototype for the practice problems //At every call Results and the status of flag is displayed to stdoutExplanation / Answer
#include <stdint.h>
#include <stdio.h>
struct flag
{
uint8_t zero;
uint8_t negative;
uint8_t carry;
uint8_t overflow;
}status;
void Signed_addition(int8_t x,int8_t y)
{
printf("signed addition is %d ",x+y);
if((x+y)==0)
{
status.zero=1;
printf("Status of zero flag is 1 ");
}
else
{
status.zero=0;
printf("Status of zero flag is 0 ");
}
if((x+y)>=0)
{
status.carry =0;
status.negative=0;
printf("Status of carry flag is 0 ");
printf("Status of negative flag is 0 ");
}
else
{
status.carry =1;
status.negative=1;
printf("Status of carry flag is 1 ");
printf("Status of negative flag is 1 ");
}
if(x+y >255 || x+y <-256)
{
status.overflow=1;
printf("Status of Overflow flag is 1 ");
}
else
{
status.overflow =0;
printf("Status of overflow flag is 0 ");
}
printf(" ");
}
void Signed_subtraction(int8_t x,int8_t y)
{
printf("signed subtraction is %d ",x-y);
if((x-y)==0)
{
status.zero=1;
printf("Status of zero flag is 1 ");
}
else
{
status.zero=0;
printf("Status of zero flag is 0 ");
}
if((x-y)>=0)
{
status.carry =0;
status.negative=0;
printf("Status of carry flag is 0 ");
printf("Status of negative flag is 0 ");
}
else
{
status.carry =1;
status.negative=1;
printf("Status of carry flag is 1 ");
printf("Status of negative flag is 1 ");
}
if(x-y >255 || x-y <-256)
{
status.overflow=1;
printf("Status of Overflow flag is 1 ");
}
else
{
status.overflow =0;
printf("Status of overflow flag is 0 ");
}
printf(" ");
}
void Unsigned_addition(uint8_t x,uint8_t y)
{
printf("Unsigned addition is %d ",x+y);
if((x+y)==0)
{
status.zero=1;
printf("Status of zero flag is 1 ");
}
else
{
status.zero=0;
printf("Status of zero flag is 0 ");
}
if((x+y)>=0)
{
status.carry =0;
status.negative=0;
printf("Status of carry flag is 0 ");
printf("Status of negative flag is 0 ");
}
else
{
status.carry =1;
status.negative=1;
printf("Status of carry flag is 1 ");
printf("Status of negative flag is 1 ");
}
if(x+y >255)
{
status.overflow=1;
printf("Status of Overflow flag is 1 ");
}
else
{
status.overflow =0;
printf("Status of overflow flag is 0 ");
}
printf(" ");
}
void Unsigned_subtraction(uint8_t x,uint8_t y)
{
printf("Unsigned subtraction is %d ",x-y);
if((x-y)==0)
{
status.zero=1;
printf("Status of zero flag is 1 ");
}
else
{
status.zero=0;
printf("Status of zero flag is 0 ");
}
if((x-y)>=0)
{
status.carry =0;
status.negative=0;
printf("Status of carry flag is 0 ");
printf("Status of negative flag is 0 ");
}
else
{
status.carry =1;
status.negative=1;
printf("Status of carry flag is 1 ");
printf("Status of negative flag is 1 ");
}
status.overflow=0;
printf("Status of Overflow flag is 0 ");
printf(" ");
}
int main()
{
Unsigned_addition(100,160);
Unsigned_subtraction(5,3);
Signed_addition(3,-5);
Signed_subtraction(5,5);
}
/*
Output:
Unsigned addition is 260
Status of zero flag is 0
Status of carry flag is 0
Status of negative flag is 0
Status of Overflow flag is 1
Unsigned subtraction is 2
Status of zero flag is 0
Status of carry flag is 0
Status of negative flag is 0
Status of Overflow flag is 0
signed addition is -2
Status of zero flag is 0
Status of carry flag is 1
Status of negative flag is 1
Status of overflow flag is 0
signed subtraction is 0
Status of zero flag is 1
Status of carry flag is 0
Status of negative flag is 0
Status of overflow flag is 0
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.