{"id":1127,"date":"2025-11-04T21:32:41","date_gmt":"2025-11-04T13:32:41","guid":{"rendered":"https:\/\/vm1.go2see.me\/?p=1127"},"modified":"2025-11-04T21:32:41","modified_gmt":"2025-11-04T13:32:41","slug":"laboratory-8-programming-risc-v","status":"publish","type":"post","link":"https:\/\/vm1.go2see.me\/?p=1127","title":{"rendered":"Laboratory 8 \u2013 Programming RISC-V"},"content":{"rendered":"<h1>Laboratory 8 \u2013 Programming RISC-V<\/h1>\n<p><strong>This lab is done individually. V2.<\/strong><\/p>\n<h2>Learning Objectives<\/h2>\n<ol>\n<li>Learn the basics of RISC-V assembly code.<\/li>\n<li>Use Linux system calls to print messages and exit.<\/li>\n<li>Write doubly-nested loops in assembly.<\/li>\n<\/ol>\n<h2>Abstract<\/h2>\n<p>In this lab, you will write assembly code with increasing complexity.<\/p>\n<h2>Equipment<\/h2>\n<p>You will use your computer and program on the cpen211-ssh RISC-V compute servers.<br \/>\nYou may wish to consult the RISC-V Instruction Set Summary (1 page) or (4 page)<br \/>\nversions.<\/p>\n<h2>Overview<\/h2>\n<p>There are 4 parts to this lab:<\/p>\n<ul>\n<li>A) Hello World<\/li>\n<li>B) Rectangles<\/li>\n<li>C) Triangles<\/li>\n<li>D) Subroutines<\/li>\n<\/ul>\n<p>Each part requires adding complexity to your program.<\/p>\n<h3>A. Hello World<\/h3>\n<ol>\n<li>Go back and repeat Lab 0 Part 1 again. It should make a lot more sense now!\n<ul>\n<li>Do both the C and Assembly parts.<\/li>\n<\/ul>\n<\/li>\n<li>Dig deeper on the assembly language part until you can confidently answer the   following questions:\n<ul>\n<li>How does the processor know where to start executing the program?<\/li>\n<li>How does the program let the processor know it exit the program? What   would happen if this wasn\u2019t done?<\/li>\n<li>What is the purpose of the <code>.equ<\/code> lines ?<\/li>\n<li>What is the purpose of the <code>.ascii<\/code> line?<\/li>\n<li>List all of the labels used in this program.<\/li>\n<li>What happens when you delete the two characters `\\n\u2019 at the end of the<br \/>\nstring? Hint: don\u2019t forget to change the string length (unlike C, strings in<br \/>\nassembly are not null-terminated by default.) These two characters<br \/>\nrepresent a single, non-printable ASCII character \u2013 what is the decimal and<br \/>\nhexadecimal value of this non-printable character?<\/li>\n<li>In Lab 0, you were asked to print a second (different) message after the first<br \/>\nmessage. Many of you skipped this step \u2013 make sure you do it this time!<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3>B. Rectangle<\/h3>\n<ol start=\"3\">\n<li>\n<p>Make a copy of hello.s, and call it rect.s, eg in Linux type:<br \/>\ncp hello.s rect.s <\/p>\n<\/li>\n<li>\n<p>In the steps below, modify rect.s incrementally to build up your solution. It is<br \/>\nstrongly advised you do not skip ahead and go to the last part, or you will find<br \/>\ndebugging very challenging.<\/p>\n<ul>\n<li><strong>Don\u2019t forget to use the \u201cgdb\u201d program to help you debug!<\/strong><\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Print an asterisk \u201c*\u201d and a new line \u201c\\n\u201d.<\/p>\n<ul>\n<li>\n<p>Modify the program so you print a single asterisk character \u201c*\u201d, then print<br \/>\na newline character \u201c\\n\u201d, then exit.<\/p>\n<\/li>\n<li>\n<p>Make sure your program works before going to the next step.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Print N asterisk characters ending with a new line.<\/p>\n<ul>\n<li>\n<p>Define a constant N using \u201c.equ\u201d with a small value like 3 or 4.<\/p>\n<\/li>\n<li>\n<p>Use a loop, storing the loop count and the loop end value in the t registers.<\/p>\n<\/li>\n<li>\n<p>Change the value of N. Make sure your program works for N=0 to 10. When<br \/>\nN=0, no stars should print and no newline should print.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Print N lines of text, where each line has N asterisks ending with a new line.<\/p>\n<ul>\n<li>\n<p>Add another loop outside of the loop in step 6, using more t registers.<\/p>\n<\/li>\n<li>\n<p>Change the value of N. Make sure your program works for N=0 to 10. When<br \/>\nN=0, no stars should print and no newline should print.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3>C. Triangles<\/h3>\n<ol start=\"8\">\n<li>\n<p>Make a copy of rect.s and call it tri.s<\/p>\n<\/li>\n<li>\n<p>Assuming the outer loop is Y=1..N (or 0..N-1, whichever you prefer), and the inner<br \/>\nloop is X=1..N (likewise, 0..N-1 is ok), modify the inner loop X so that it only prints<br \/>\nup to Y stars, that is X=1..Y. This should print a simple right-angle triangle where<br \/>\nthe diagonal is exactly 45 degrees.<\/p>\n<ul>\n<li><strong>Don\u2019t forget to use the \u201cgdb\u201d program to help you debug!<\/strong><\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Modify the loop structure of your assembly code to match the C code below.   (NOTE: do NOT simply write the C code version and compile it.) In this implementation, you will define two constants NY and NX which define the    number of lines of text (NY) and the number of asterisks to print (NX) at the base  of the triangle when Y=NY.<\/p>\n<pre><code class=\"language-c\">void tri( int NX, int NY )\n{\n    int x_max = NX;\n    for( int y=0; y < NY; y++ ) {\n        for( int x=0; x < x_max; x += NY ) {\n        printf(\"*\");\n        }\n        printf(\"\\n\");\n        x_max += NX;\n    }\n}<\/code><\/pre>\n<ul>\n<li>\n<p>Running tri(4,8), where NX=4 and NY=8, should print this:<\/p>\n<pre><code>*\n*\n**\n**\n***\n***\n****\n****<\/code><\/pre>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<p>Running tri(8,4), where NX=8 and NY=4, should print this:<\/p>\n<pre><code>**\n****\n******\n********<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Test your program with a variety of values for NX and NY.<\/p>\n<\/li>\n<\/ol>\n<h3>D. Subroutines<\/h3>\n<ol start=\"12\">\n<li>Divide your program into a main part, and a subroutine for tri(). Pass the values NX<br \/>\nand NY as arguments in a0 and a1, respectively. The main part should only set up<br \/>\nthe arguments, call tri(), and then exit. Note: you can rely upon the value of the<br \/>\nstack pointer (sp) being set up for you automatically by the compilation system.<\/li>\n<li>Change your main part to call tri() twice before exiting, eg: tri(5,10) and tri(20,10).<\/li>\n<\/ol>\n<h3>SUBMISSION REQUIREMENTS<\/h3>\n<ol start=\"14\">\n<li>TBD<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Laboratory 8 \u2013 Programming RISC-V This lab is done indi&#8230; &raquo; <a class=\"read-more-link\" href=\"https:\/\/vm1.go2see.me\/?p=1127\">\u95b1\u8b80\u5168\u6587<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1127","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/posts\/1127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1127"}],"version-history":[{"count":2,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/posts\/1127\/revisions"}],"predecessor-version":[{"id":1129,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/posts\/1127\/revisions\/1129"}],"wp:attachment":[{"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}