Laboratory 1 Assembly Language and the RISC-V ISA

The RISC-V Assembler and Runtime Simulator (RARS) can be downloaded from:

https://github.com/TheThirdOne/rars/releases/tag/v1.5

All information is in pdf file

 

DOWNLOAD FILES HERE

Laboratory 1 Assembly Language and the RISC-V ISA

Page 1of 6School of EngineeringEEET2261–Computer Architecture and OrganisationLaboratory 1Assembly Language and the RISC-V ISA1Aims(i)To develop an appreciation for the abstraction provided by a microprocessor’s Instruction Set Architecture (ISA).(ii)To use theRISC-V specification to understand thefunctional operation of the RISC-V ISA.(iii)To implement common algorithms typical of those suited to optimisation through direct assembly language programmingusing the RV32I base ISA.(iv)To develop an appreciation for theconstraints imposed on software by the underlying ISA.(v)To utilise the RISC-V assembler and runtime simulatorto implement, assembleand evaluate algorithms in assembly language

IntroductionYou are expected to work individually. Each student must submit asingle .zip archive containing their assembly code files according to the supplied project template.This laboratory project will run for two weeks,withsubmission dueat the end of week 3(electronicsubmission via the subject Canvas website).The so-called Internet of Things (IoT) has given rise to an explosion in the development and deployment of small, cheap, and often resource constrained, embedded systems. To maximise performance, these systems are good candidates for code optimisation through direct assembly language programming. In this laboratory, you will implement in RISC-V assembly language, a number of algorithms typical of those required in such embedded systems. Specifically, you will make use of the RV32I subset of the RISC-V instruction set architecture (ISA)[1,2]. To evaluate your submitted solutions, your code will be assembled and linked to a set of test routines, and its functionality assessed using the RISC-V Assembler and Runtime Simulator (RARS) version 1.5 [3].RARS may be downloaded from the link below (see [3]) as a.jar file. You will need Java8 or later to run it.RARS is comprised of a simple text editor for viewing and writing assembly language code(although you may use any text editor you prefer, outside of RARS), a RISC-Vassembler and a register level runtime simulator that supports single stepping and breakpoints for debugging.

 

To get started, download the project template from the subject Canvas website.The project template (provided as a single .zip archive) contains code stubs together with example test routinesfor each of the assembly programming tasks described below.You will complete these code stubs and must then archive (.zip) and submit your project directory for assessment.3Loops and conditionals (Linear search)The project template contains twofiles:min.asmand max.asm.Each file contains a code stubdefining a procedureto compute the minimum and maximum, respectively,of an array of signed 32-bit integersofarbitrary length. Your task is to complete eachof these code stubsto return the minimum and maximum value using a linear search of the supplied array. Your implementation of each of these procedures must satisfy the following constraints:1.The array of integers is stored in memory in a contiguous range of addresses. When each procedure(minor max) is called, the starting address of the array will be stored in a0and the number of integers in the array will be stored in a1.2.Each procedureshould return the index of its target (i.e., the minimum or maximumvalue) in a0.3.Eachprocedureshould end with a retpseudo-instruction.

Note:The returned value should be the element index, not an addresses orbyte offset.Do not rename the files (min.asmand max.asm) or change the name of the procedures (minand max) defined in the supplied code stubs.To test your solution in RARS, you may use theexample test routineststmin.asmand tstmax.asm,providedinthe testsdirectory.Discussion questions:Evaluate the computational cost ofyour solution, in terms of the number of instructions executed.How does this vary with the length of the supplied array?Hint: You can use RARS to confirm your estimate of the number of instructions executed using theInstruction Counter, located under the Tools menu or by using the iccommand line argument.4Procedure calls(Quicksort)The cost of many operationsthat involve linear search can besignificantly reduced by first sorting the array to be searched. Consider the case of finding the minimum and maximum: for a sorted arrayno search is required (the minimum and maximum are simplythe first and last elementsin the array). Searching for an arbitrary value in anarray is another example which can benefit significantly from sorting of the array before searching, especially if many searches for different values, are required.The project template contains an additional file: quicksort.asm. This file contains a code stub defining a procedure to sort an array of signed 32-bitintegers of arbitrary length into ascending order.Yourtask is to complete this code stub to sort the supplied array using the Quicksort algorithm [4].Your implementation of the sortprocedure must satisfy the following constraints:1.The array of integers is stored in memory in a contiguous range of addresses. When the sortprocedure is called, the starting address of the array will be stored in a0and the element indices of the start and end of the region to be sorted will be stored in a1and a2, respectively.2.The sort procedure should modify the array in place, and does not need to return any value.3.The sortprocedure should end with a retpseudo-instruction.

EEET2394Laboratory2–VR Sensor Signal ProcessingPage 3of 6algorithmsort(A, lo, hi) isiflo < hi thenp:= partition(A, lo, hi)sort(A, lo, p -1)sort(A, p + 1, hi)algorithmpartition(A, lo, hi) ispivot:= A[hi]i:= loforj:= lo tohi doifA[j] < pivot thenswap A[i] with A[j]i:= i + 1swap A[i] with A[hi]returniFig.1.The Quicksort algorithm [4].Hint:yourimplementation of the sortprocedure should be recursive (see Fig.1) and make use of the partitionprocedure defined in the code stub.Do not rename the file (sort.asm) or change the name of the procedure (sort) defined in the supplied code stub.To test your solution in RARS, you may use the example test routine tstsort.asmprovided in the testsdirectory.Discussion questions:Evaluate the computational cost or your solution, in terms of the number of instructions executed. How does this vary with the lengthof the supplied array?Can you reduce this cost?5Constraints of the RV32I ISA (Binary search)The file search.asmin the project template contains a code stub defining a procedure to search a sorted array of unsigned 32-bit integers. Yourtask is to complete this code stub to search the supplied array using a binary search algorithm [5]. Your implementation of the searchprocedure must satisfy the following constraints:1.The array of integers is stored in memory in a contiguous range of addresses. When the searchprocedure is called, the starting address of the array will be stored in a0and the number of integers in the array (constrained to be a power of 2) will be stored in a1. The target of the search will be stored in a2.2.If the target is found in the array, the search procedure should return the index of the target in a0.If the target is not found in the array, a0should be set to a value of -1.3.The searchprocedure should end with a retpseudo-instruction.The binary search algorithm (Fig.5) is also known as the half-interval search algorithm. It first compares the target with the middle element of the array. Based on this comparison half of the sorted array is eliminated, and the search proceeds on the remaining half, again comparing the target with the middle value (of the remaining half). Binary search is therefore based on division, by 2 –to halve the search interval after each step. In the RV32I ISA, this division must be achieved by use of aright-shift instruction (e.g., srai). To facilitate this division, and to simplify implementation of the binary search algorithm, here the length of the array is assumed (or rather constrained) to be a power of 2
functionbinary_search(A, n, T) isL:= 0R:= n − 1whileL ≤ R dom:= L + ((R-L) ≫2)ifA[m] < T thenL:= m + 1else ifA[m] > T thenR:= m − 1else:returnmreturnunsuccessfulFig.2.The binary searchalgorithm [4].Do not rename the file (search.asm) or change the name of the procedure (search) defined in the supplied code stub.To test your solution in RARS, you may use the example test routine tstsearch.asmprovided in the testsdirectory.Discussion questions:As noted above, here the length of the array to be search is constrained to be a power of 2. How could this constraint be relaxed?6ChallengetaskIn the searchprocedure implemented above, the length of the array to be searchedwas constrained tobe a power of 2.In this ChallengeTask, your task is to modify your implementation of the searchprocedure to accept (and search) arrays of arbitrary length.7Assessment processOnce you have completed (and tested) the code stubs described above (min.asm,max.asm, sort.asmand search.asm)archive the entire project directory into a single .zip archive and submit this archive via the subject Canvas website.Failure to submit your code archive will incur a 30% penalty.Your completed code stubs will be assembled and linked with a set of test routines designed to test their functionality using the RISC-V Assembler and Runtime Simulator (RARS) version 1.5 [3]. These test routines are similar but not identical to those provided in the project template. Thetest routines provided in the project template are intended to serve as examples for testing your code. They provide only minimal testing of your code. You are encouraged to consider different scenarios and challenging edge cases, and to create any additional testing or debugging routines to test those cases.Once you have completed the assessable tasksplease prepare a short demonstration so the Laboratory Demonstrator can evaluate your solution. You should be able to step through any of your code in RARS, and to predict how the register or memory contents will change before executing each instruction.You should arrange to demonstrate your code before the end of your laboratory session in Week 3.
EEET2394Laboratory2–VR Sensor Signal Processing Page 5of 68

References[1]The RISC-V Instruction Set Manual: Volume I: Unprivileged ISA, https://github.com/riscv/riscv-isa-manual/releases/tag/draft-20200717-259025b, [online], Accessed 2020-07-18.[2]Open RISC-V Reference Gard, http://riscbook.com/greencard-20181213.pdf, [online], Accessed 2020-07-18.[3]The RISC-V Assembler and Runtime Simulator (RARS), https://github.com/TheThirdOne/rars/releases/tag/v1.5,[online], Accessed 2020-07-18.[4]Quicksort, https://en.wikipedia.org/wiki/Quicksort, [online], Accessed 2020-07-27.[5]Binary search algorithm, https://en.wikipedia.org/wiki/Binary_search_algorithm, [online], Accessed 2020-07-29.

EEET2394Laboratory2–VR Sensor Signal ProcessingPage 6of 6Appendix: RISC-V Calling conventionRegisterAliasDescriptionSaverx0zero––x1raReturn address/link registerCallerx2spStack pointerCalleex3gpGlobal pointer–x4tpThread pointer–x5–x7, x28–x31t0–t2, t3 –t6TemporariesCallerx8–x9, x18–x27s0 –s1, s2 –s11SavedCalleex10–x17a0 –a7Arguments/resultsCaller
.586
.MODEL FLAT
%include “io.h”
.DATA
number1 DWORD ?
number2 DWORD ?
prompt1 BYTE “Enter first number”, 0
prompt2 BYTE “Enter second number”, 0
string BYTE 40 DUP (?)
resultLbl BYTE “The sum is”, 0
sum BYTE 11 DUP (?), 0
.CODE
_MainProc PROC:
mov ebp, esp; for correct debugging
;write your code here
input prompt1, string, 40 ;read ascii characters
atod string ;convert to integer
mov number1, eax ;store in memory
input prompt2, string, 40 ;repeat for second number
atod string
mov number2, eax
mov eax, number1 ;first nubmer to eax
add eax, number2 ; add second number
dtoa sum, eax ;convert to ASCII characters
output resultLbl, sum ; output label and sum
mov eax, 0 ;exit with return code 0
ret
_MainProc ENDP

END ;end of source code

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.