write the assembly program Question 2 Transform the followin
write the assembly program
Question 2
Transform the following “C” program into assembly for the 6808 Microcontroller.
• Remember when converting this “C” code to follow best practices covered in terms of implementing subroutines in assembly
o This means that parameters are passed on the stack to subroutines
o Return values are passed back on the stack
o You clean up the stack after every use (every subroutine call) to prepare it for the next call
Keep in mind that once your translated this program into assembly – you can step through it to ensure that it behaves correctly and calculates the expected results.
/* -------------------------------------------------------------
PURPOSE: This program will take 2 operand values and
calculate the sum and difference of the values
------------------------------------------------------------- */
// This function calculates the sum of 2 operands called \"A\" and \"B\"
Int calculateSum(int A, int B)
{
return (A + B);
}
// This function calculates the difference between 2 operands by negating B and adding it to A
int calculateDifference(int A, int B)
{
return (calculateSum(A, -B)); // reuse the calculateSum function
}
void main(void)
{
int firstOperand = 18; // to be stored at address $80 within the 6808
int secondOperand = 8; // to be stored at address $81 within the 6808
int sum = 0; // to be stored at address $84 within the 6808
int difference = 0; // to be stored at address $86 within the 6808
// call the calculateSum function in order to calculate the total and store it in the \"sum\" variable
sum = calculateSum(firstOperand, secondOperand);
// call the calculateDifference function in order to calculate the difference and store it in the \"difference\" variable
difference = calculateDifference(firstOperand, secondOperand);
}
Solution
calculateSum:
pushq R1
movq R2,R1
movl R3,R1
movl R4, R1
movl R1, R5
movl R1, R6
addl R5, R6
popq R1
ret
calculateDifference:
pushq R1
movq R2, R1
subq 8,R2
movl R3, R1
movl R4, R1
movl R1, R6
negl R6
movl R6, R5
movl R1, R6
movl R5, R4
movl R6, R3
call calculateSum
leave
ret
main:
pushq R1
movq R2, R1
subq 16, R2
movl 18, R1
movl 8, R1
movl 0, R1
movl 0, R1
movl R1, R5
movl R1, R6
movl R5, R4
movl R6, R3
call calculateSum
movl R6, R1
movl R1, R5
movl R1, R6
movl R5, R4
movl R6, R3
call calculateDifference
movl R6, R1
nop
leave
ret


