w1.2: finish assignment1.2
This commit is contained in:
22
assignment_assembly/assignment2/main.c
Normal file
22
assignment_assembly/assignment2/main.c
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
/* main.c simple program to test assembler program */
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
extern int test(int a, int b);
|
||||||
|
extern unsigned int multiply(unsigned int a, unsigned int b);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
extern void initialise_monitor_handles(void);
|
||||||
|
initialise_monitor_handles();
|
||||||
|
|
||||||
|
int a = test(3, 5);
|
||||||
|
printf("Result of test(3, 5) = %d\n", a);
|
||||||
|
|
||||||
|
unsigned int b = multiply(3, 5);
|
||||||
|
printf("Result of multipy(3, 5) = %d\n", b);
|
||||||
|
|
||||||
|
b = multiply(3, 0);
|
||||||
|
printf("Result of multipy(3, 0) = %d\n", b);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
50
assignment_assembly/assignment2/mulOne.c
Normal file
50
assignment_assembly/assignment2/mulOne.c
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
unsigned int multiply(unsigned int a, unsigned int b);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Multiplies two unsigned int values
|
||||||
|
* @param unsigned int a
|
||||||
|
The value of the multiplier a (0-4294967295)
|
||||||
|
* @param unsigned int b
|
||||||
|
The value of the multiplicand b (0-4294967295)
|
||||||
|
* @return unsigned int
|
||||||
|
The answer of a times b given that a * b < 4294967295
|
||||||
|
*/
|
||||||
|
|
||||||
|
// // This function must be implemented in LEGv7 Pinky assembly
|
||||||
|
// unsigned int multiply(unsigned int a, unsigned int b)
|
||||||
|
// {
|
||||||
|
// unsigned int m = 0;
|
||||||
|
// for (unsigned int i = 0; i != a; i++)
|
||||||
|
// {
|
||||||
|
// m = m + b;
|
||||||
|
// }
|
||||||
|
// return m;
|
||||||
|
// }
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
extern void initialise_monitor_handles(void);
|
||||||
|
initialise_monitor_handles();
|
||||||
|
|
||||||
|
unsigned int a[] = {0, 1, 0, 1, 2000, 2, 10000, 1 , 65535 };
|
||||||
|
unsigned int b[] = {0, 0, 1, 1, 2, 65535, 65535, 4294967295u, 65535 };
|
||||||
|
unsigned int r[] = {0, 0, 0, 1, 4000, 131070, 655350000, 4294967295u, 4294836225u};
|
||||||
|
|
||||||
|
for (size_t i = 0; i != sizeof(a)/sizeof(a[0]); i++)
|
||||||
|
{
|
||||||
|
printf("%u x %u: ", a[i], b[i]);
|
||||||
|
unsigned int result = multiply(a[i], b[i]);
|
||||||
|
unsigned int correct = r[i];
|
||||||
|
if (result != correct)
|
||||||
|
{
|
||||||
|
printf("Failed, function returned %u but the correct answer is %u\n", result, correct);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Passed, %u\n", result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -5,14 +5,14 @@
|
|||||||
.text
|
.text
|
||||||
.thumb_func
|
.thumb_func
|
||||||
multiply:
|
multiply:
|
||||||
MOVS.N R2, #0
|
MOVS.N R2, #0 // set result to 0
|
||||||
ADDS.N R1, #0
|
CMP.N R1, #0
|
||||||
BEQ.N =exit
|
BEQ.N exit // goto exit if b == 0
|
||||||
loop:
|
loop:
|
||||||
ADDS.N R2, R0, R2
|
ADDS.N R2, R0, R2 // add a to result
|
||||||
SUBS.N R1, #1
|
SUBS.N R1, #1 // substract 1 from b
|
||||||
BNE.N =exit
|
BEQ.N exit // goto exit if b == 0
|
||||||
B.N =loop
|
B.N loop // loop otherwise
|
||||||
exit:
|
exit:
|
||||||
MOVS.N R0, R2
|
MOVS.N R0, R2 // move result to R0
|
||||||
BX.N LR
|
BX.N LR
|
||||||
|
|||||||
@@ -25,4 +25,16 @@ Hierna werken het nogsteed zoals verwacht.
|
|||||||
|
|
||||||
## Writeting a simple multiplication program
|
## Writeting a simple multiplication program
|
||||||
|
|
||||||
|
Om te beginnen heb ik het project van de vorige assignment. Hier heb ik aan *main.c* een paar simpele regels toegevoegd om de basis werking te testen.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Pas toen ik het werkend had kwam ik achter dat ik `a` en `b` heb omgedraaid. Dit maakt gelukkig niet uit in een vermedigvuldiging. Daarnaast heb ik ook `i` weg gewerkt door i.p.v. `i` te verhogen en daarna te vergeleiken met `b` heb ik `b` verlaagd tot het 0 is.
|
||||||
|
|
||||||
|
Dit is mijn implementatie:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## A smarter tail recursive multiply algorithm
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user