Escolar Documentos
Profissional Documentos
Cultura Documentos
Convert main function given in file c2lc3.c from C to LC-3 assembly language.
C2lc3.c
int SUM(int list[], int n);
int main()
{
int sum, array[5]={1,1,2,2,3}
sum = SUM(array, 5);
printf(%d\n, sum);
return 0;
}
int SUM(int list[], int n)
{
int i, sum = list[0];
for (i = 1; i < n; i++)
sum += list[i];
return sum;
}
Subroutine SUM has been already converted for you. Its source code is in sum.asm. You may
assume this subroutine returns the sum of the elements of the array passed into it. DO NOT
WRITE THE CODE FOR SUM SUBROUTINE, DO NOT MODIFY IT.
Your program should be saved in main.asm file. To receive full credit, it should correctly run in
lc3sim (or lc3sim-tk). You must use the run-time stack convention presented in the textbook
and the lectures to invoke the subroutine from main. Note that printf call is already
implemented for you. Also, you do not need to save any registers.
You should initialize the current stack pointer (R6) and frame pointer (R5) to point to
memory address stored under RTSTACK before using the run-time stack. Note that main
does not require bookkeeping info to be pushed onto the stack. But you should allocate any
memory needed for mains local variables on the stack as part of mains setup.
Next page contains the code provided to you in main.asm. You only need to implement
sections labeled with IMPLEMENT ME. Do not modify any other parts!
Your code will be graded for functionality of the main function as well as the correct
construction of the activation record in the run-time stack. Consequently, you MAY NOT perform
any optimizations that impact the contents of the stack. For example, SUM saves R7 even
though it does not make a subroutine call. ALL local variables used in the C function should be
stored in the run-time stack appropriately. The local variables may NOT be stored locally in
memory reserved by .FILL, .BLKW, etc. You may receive zero points if your code does not
assemble or does not behave as specified.
main.asm
; ECE220 Exam 2
; Main Assembly Code
; Use run-time stack convention for function's activation records
;
R5 is the frame pointer
;
R6 is the stack pointer
.ORIG x3000
; IMPLEMENT ME: Setup main's top of the stack pointer
; IMPLEMENT ME: Setup main's frame pointer
; IMPLEMENT ME: Allocate and initialize main's local variables
; IMPLEMENT ME: Prepare function call
; call SUM subroutine (already implemented for you)
LD R0, SUM
JSRR R0
; IMPLEMENT ME: get result and pop arguments from the stack
; !!!DO NOT EDIT PAST THIS LINE!!!
; control back to main
LDR R0, R5, #0 ; printf(...
LD R1, ASCII
ADD R0, R0, R1
OUT
LD R0, NEWLINE
OUT
HALT ; return 0;
RTSTACK
SUM
NEWLINE
ASCII
.END
.FILL
.FILL
.FILL
.FILL
x30FF
x3080
x000A
x0030
sum.asm
; !!! DO NOT EDIT THIS FILE
;
; ECE220 Exam 2
; Sum Assembly Code
!!!
#-1
#-1
#0
#-1
#0
#-1
#-1
#-1
#4
#0
#-1
SUM
R0, #0
R0, #1
R5, #0 ; i = 1
LOOP
LDR R0, R5, #0 ; R0 <- i
LDR R1, R5, #5 ; R1 <- n
NOT R1, R1
ADD R1, R1, #1 ; R1 <- -n
ADD R2, R0, R1
BRzp DONE
LDR
LDR
ADD
LDR
R1,
R0,
R1,
R0,
R5,
R5,
R1,
R1,
#4
#0
R0
#0
; R2 <- i-n
;
;
;
;
address of list[0]
R0 <- i
address of list[i]
R0 <- list[i]
R6,
R6,
R6,
R6,
R6,
#2
#0
#1
#0
#1