0% found this document useful (0 votes)
3 views3 pages

Assignment4 Winter2024 Sol

This document outlines Assignment 4 for COEN 311, which requires students to write ARM assembly code for three tasks: calculating the greatest common divisor (gcd), generating the Fibonacci sequence, and concatenating two strings. Each task has specific requirements and grading criteria, with the gcd task worth 20 points, the Fibonacci task worth 40 points, and the string concatenation task also worth 40 points. The assignment is due on April 10, 2024.

Uploaded by

sasa
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)
3 views3 pages

Assignment4 Winter2024 Sol

This document outlines Assignment 4 for COEN 311, which requires students to write ARM assembly code for three tasks: calculating the greatest common divisor (gcd), generating the Fibonacci sequence, and concatenating two strings. Each task has specific requirements and grading criteria, with the gcd task worth 20 points, the Fibonacci task worth 40 points, and the string concatenation task also worth 40 points. The assignment is due on April 10, 2024.

Uploaded by

sasa
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/ 3

COEN 311 – Computer Organization and Software

Department of Electrical and Computer Engineering


Assignment 4, Winter 2024

Due: Sunday, April 10, 2024


In this assignment, you will answer the following questions. Write your answer in the exact place:

Your Information:
First Name:

Last Name:

Student ID:

Grade:

Question 1 Grade
Write code to calculate the greatest common divisor (gcd) using ARM assembly language. The 20
first parameter a and the second parameter b are available in memory in the locations opa and
opb respectively. Save the result to a memory location whose address is gcd.

.global main
.text
main:
LDR R0, =opa @ Load the address of opa into R0
LDR R1, =opb @ Load the address of opb into R1

@ Euclidean algorithm
LDR R0, [R0] @ Load the value of a from memory into R0
LDR R1, [R1] @ Load the value of b from memory into R1

gcd_loop:
CMP R1, #0 @ Check if b is equal to 0
BEQ gcd_end @ If b is 0, exit the loop

MOV R2, R0 @ Copy the value of a to R2


MOV R0, R1 @ Move b to a
MOV R1, R2 @ Move the copy of a to b

SUB R0, R0, R1 @ Subtract b from a

B gcd_loop @ Repeat the loop

gcd_end:
MOV R2, R0 @ Move the final value of a (gcd) to R2

1
LDR R0, =gcd @ Load the address of gcd into R0
STR R2, [R0] @ Store the gcd value to the memory location
Here: b Here

.data
opa: .word 48 @ First parameter a
opb: .word 36 @ Second parameter b
gcd: .space 4 @ Memory location to store the result

Question 2 Grade
Write ARM assembly code to calculate the Fibonacci sequence. The main function calls the fibo 40
function to calculate the Fibonacci sequence up to the nth element. The value n is loaded into
R0 as the parameter for the fibo function. Save the final result in R0.

.global main

.text
main:
MOV R0, #10 @ Set the value of n (number of elements)
BL fibo @ Call the fibo function
Here: B Here

fibo:
PUSH {R1} @ Save R1 on the stack

CMP R0, #0 @ Check if n is 0


MOVEQ R0, #0 @ Return 0 if n is 0
BEQ fibo_end

CMP R0, #1 @ Check if n is 1


MOVEQ R0, #1 @ Return 1 if n is 1
BEQ fibo_end

SUB R1, R0, #1 @ Calculate fibo(n - 1)


BL fibo @ Recursive call to fibo(n - 1)
MOV R2, R0 @ Save fibo(n - 1) to R2

SUB R0, R0, #2 @ Calculate fibo(n - 2)


BL fibo @ Recursive call to fibo(n - 2)
ADD R0, R0, R2 @ Add fibo(n - 1) to fibo(n - 2)

fibo_end:
POP {R1} @ Restore R1
BX LR @ Return to the calling code

.end

2
Question 3 Grade
Write a subroutine STRCAT to concatenate two strings in ARM assembly language. The main 40
program passes the addresses of both strings by registers to the subroutine. The second string
is concatenated to the first string. The addresses of the strings are str1 and str2 and the
address of the new string is str3.

.global main
.text
main:
LDR R0, =str1 @ Load the address of str1 into R0
LDR R1, =str2 @ Load the address of str2 into R1
LDR R2, =str3 @ Load the address of str3 into R2
BL STRCAT @ Call the STRCAT subroutine
B.

STRCAT:
PUSH {LR, R0, R1, R2} @ Save the return address, R0, R1, and R2 on the stack

@ Copy the first string (str1) to the result string (str3)


MOV R2, R0 @ Move the address of str1 to R2
copy_loop:
LDRB R3, [R2] @ Load a character from str1
STRB R3, [R1] @ Store the character in str3
ADD R2, R2, #1 @ Increment the address of str1
ADD R1, R1, #1 @ Increment the address of str3
CMP R3, #0 @ Check if the end of str1 is reached
BNE copy_loop @ Repeat until the null terminator is encountered

@ Append the second string (str2) to the result string (str3)


MOV R2, R1 @ Move the address of str2 to R2
append_loop:
LDRB R3, [R2] @ Load a character from str2
STRB R3, [R1] @ Store the character in str3
ADD R2, R2, #1 @ Increment the address of str2
ADD R1, R1, #1 @ Increment the address of str3
CMP R3, #0 @ Check if the end of str2 is reached
BNE append_loop @ Repeat until the null terminator is encountered

@ Store the null terminator at the end of the concatenated string (str3)
STRB R3, [R1]
POP {R2, R1, R0, PC} @ Restore R0, R1, R2, and the return address to return
; note we can use BX LR instead of POP{PC}

.data
str1: .asciz "Hello, "
str2: .asciz "world!"
str3: .space 100 @ Reserve space for the concatenated string
3

You might also like