CS 2110 Homework 5 Intro to Assembly Solved

$ 24.99
Category:

Description

Avaneesh Naren, Allison Fain, Alice Zeng, Irene Feijoo
Version 1.0
Contents
1 TLDR 3
1.1 Debugging Assembly 3
1.2 Summation 3
1.3 Build Minimum Array 3
1.4 Binary String to Int 3
1.5 Five Character Strings 4
2 Overview 5
2.1 Purpose 5
2.2 Task 5
2.3 Criteria 5
3 Detailed Instructions 6
3.1 Notes 6
3.2 Part 1: Summation 6
3.3 Part 2: Build Minimum Array 7
3.4 Part 3: Binary String to Int 8
3.5 Part 4: Five Character Strings in a paragraph 9
4 Deliverables 10
5 Running the Autograder and Debugging LC-3 Assembly 11
6 Appendix 14
6.1 Appendix A: ASCII Table 14
6.2 Appendix B: LC-3 Instruction Set Architecture 15
6.3 Appendix C: LC-3 Assembly Programming Requirements and Tips 16
7 Rules and Regulations 17
7.1 General Rules 17
7.2 Submission Conventions 17
7.3 Submission Guidelines 17
7.4 Syllabus Excerpt on Academic Misconduct 17
7.5 Is collaboration allowed? 18

1 TLDR
1.1 Debugging Assembly
4 For information on debugging or the autograder, jump to the debugging section. Please note that Docker must be running, but it doesn’t need to be within the image.
1.2 Summation
Given a value x, calculate the sum 1+2+…+x−1+x. Store the final sum at the label “ANSWER”. The pseudocode can be found in section 3.2.
Below is a list of what we have provided you for this method.
• label named “X” where the value will be stored
• label named “ANSWER” where you will store your final sum
Please note that since x is a label, you will need to load the label to retrieve the data. Additionally, if the value of x is less than or equal to 0, you should store 0 at the “ANSWER” label.
1.3 Build Minimum Array
Given two arrays of the same length, construct a third array that contains the minimum value at each index. Example, [2,−3,6],[−2,5,2] → [−2,−3,2]. The pseudocode can be found in section 3.3.
Below is a list of what we have provided you for this method.
• label named “A” that holds the address of the first array
• label named “B” that holds the address of the second array
• label named “C” that will hold the address of your array that contains the minimum values
• label named “LEN” that holds the size of the arrays
1.4 Binary String to Int
Given a 2s-complement binary number, convert it to a decimal integer and store it at the address of the label “RESULTIDX”. The pseudocode can be found in section 3.4 Below is a list of what we have provided you for this method.
• label named “BINARYSTRING” that holds the binary string
• label named “LENGTH” that holds the length of BINARYSTRING
• label named “RESULTIDX” that will hold the decimal integer of BINARYSTRING
• label named “ASCII” that holds -48
1.5 Five Character Strings
Given a string sentence, calculate the number of 5 character strings within the sentence. Special characters do not have to be treated differently. For instance, strings like “can’t” and “cans,” are both considered 5 character strings. The pseudocode can be found in section 3.5.
Below is a list of what we have provided you for this method.
• the ASCII value of the space character
• label named “STRING” that holds the address of the string that you will evaluate
• label named “ANSWER” that will hold the number of five character strings that you detect
2 Overview
2.1 Purpose
So far in this class, you have seen how binary or machine code manipulates our circuits to achieve a goal. However, as you have probably figured out, binary can be hard for us to read and debug, so we need an easier way of telling our computers what to do. This is where assembly comes in. Assembly language is symbolic machine code, meaning that we don’t have to write all of the ones and zeros in a program, but rather symbols that translate to ones and zeros. These symbols are translated with something called the assembler. Each assembler is dependent upon the computer architecture on which it was built, so there are many different assembly languages out there. Assembly was widely used before most higher-level languages and is still used today in some cases for direct hardware manipulation.
2.2 Task
The goal of this assignment is to introduce you to programming in LC-3 assembly code. This will involve writing small programs, translating conditionals and loops into assembly, modifying memory, manipulating strings, and converting high-level programs into assembly code.
You will be required to complete the four functions listed below with more in-depth instructions on the following pages:
1. summation.asm
2. buildMinArray.asm
3. binaryStringToInt.asm
4. fiveCharacterStrings.asm
2.3 Criteria
Your assignment will be graded based on your ability to correctly translate the given pseudocode into LC-3 assembly code. Check the deliverables section for deadlines and other related information. Please use the LC-3 instruction set when writing these programs. More detailed information on each instruction can be found in the Patt/Patel book Appendix A (also on Canvas under “LC-3 Resources”). Please check the rest of this document for some advice on debugging your assembly code, as well some general tips for successfully writing assembly code.
You must obtain the correct values for each function. While we will give partial credit where we can, your code must assemble with no warnings or errors (Complx will tell you if there are any). If your code does not assemble, we will not be able to grade that file and you will not receive any points. Each function is in a separate file, so you will not lose all points if one function does not assemble. Good luck and have fun!
3 Detailed Instructions
3.1 Notes
• The algorithms presented for each operation are not meant to be the most efficient.
• Be wary of the differences between instructions like LD and LEA. When you have an answer, make sure you’re storing to the correct address. Trace through your code on Complx if you’re not sure if you’re using the correct instruction. • Debugging via Complx helps tremendously. Eyeballing assembly code can prove to be very difficult. It helps a lot to be able to trace through your code step-by-step, line-by-line, to see if each assembly instruction does what you expected.
• You can check if far-away addresses contain expected values in Complx by going to View >> GoTo Address.
3.2 Part 1: Summation
To start you off with this homework, we are implementing a function that takes an integer x, and calculates the summation of x. Store the result of the operation at the label ANSWER. Argument ”x” is stored in a label, and you will need to load it from there to perform this operation. Similarly, “ANSWER” reserved 1 memory space for you to store the summation of x. Note that if ”x” is less than or equal to 0, return 0. Implement your assembly code in summation.asm.
For example, summation of 4 is 4 + 3 + 2 + 1 = 10.
Suggested Pseudocode:

int x = 6; (sample integer) int sum = 0; while (x > 0) { sum += x;
x–;
}
mem[ANSWER] = sum;
Given x = 6, the above code should store 21 at label “ANSWER”, since the given x will yield the summation
6 + 5 + 4 + 3 + 2 + 1 = 21.
3.3 Part 2: Build Minimum Array
For the second assembly program, you are given two integer arrays A and B of the same length. We want you to fill in a third array C of the same length where the ith element of C is the minimum of the ith element in A and the ith element of B.
For instance, if A = [-4, 2, 6] and B = [4, 7, -2], then C[i] = min(A[i], B[i]). Hence, C = [-4, 2, -2].
The label LEN will contain the size of arrays A and B. The labels A, B, and C will contain the addresses at which the arrays A, B, and C (respectively) begin in memory.
Your resulting array should be stored in memory, beginning at the address stored in label C.
Implement your assembly code in buildMinArray.asm
Suggested Pseudocode:

int A[] = {-4, 2, 6}; (sample array) int B[] = {4, 7, -2}; (sample array) int C[3]; (sample array) int length = 3; (sample length of above arrays)
int i = 0; while (i < length) { if (A[i] < B[i]) { C[i] = A[i];
} else {
C[i] = B[i];
} i++;
}
3.4 Part 3: Binary String to Int
For the third assembly program, you are given a twos-complement binary string. We want you to convert the twos-complement binary string into an integer value. Please note that we give you a label that contains the ASCII value of 0 to help you with the conversion.
For instance,
“11111100” should convert to a -4
“00100001” should convert to a 33
Your converted value should be stored in memory at the address stored in label RESULTIDX.
Implement your assembly code in BinaryStringToInt.asm Suggested Pseudocode:

String binaryString = “00010101”; (sample binary string) int length = 8; (sample length of the above binary string) int base = 1; int value = 0; int i = length – 1; while (i >= 0) { int x = binaryString[i] – 48; if (x == 1) { if (i == 0) {
value -= base;
} else { value += base;
}
} base += base; i–;
}
mem[mem[RESULTIDX]] = value;
Given a binary string(“00010101”), its length(8), and a location to store (x4000), this code should store 21 in memory address x4000.
3.5 Part 4: Five Character Strings in a paragraph
In the final part of this assignment, we want you to find the number of five letter words in a given null terminated string.
The label STRING will contain the address of the first character in our string. Keep in mind that a string is just an array of characters that end with a null-termination character (‘’). For your program, you are given one constant to help you called SPACE, which is the negative value of ‘space’ in ASCII (-32).
Store the result of the operation at the label ANSWER.
IMPORTANT
• Special characters do not have to be treated differently. For instance, strings like “can’t” and “sure,” are considered 5 character strings.
Implement your assembly code in fiveCharacterStrings.asm Assume that the last character of every string is a space.
NOTE:
• 0 is the same as ‘’
• 0 is different from ‘0’
• “ ” in ASCII is 32 (the space character)
Suggested Pseudocode:

int count = 0; (keep count of number of 5-letter words) int chars = 0; (keep track of length of each word) int i = 0; (indexer into each word)
String str = “I really love CS 2110 and using wires and code!”; (sample string) while (str[i] != ‘’) { if (str[i] != ‘ ’) { chars++;
} else { if (chars == 5) { count++;
} chars = 0;
} i++;
}
mem[ANSWER] = count;
The above code should store 3 at the label ANSWER from the 5 character strings: “using”, “wires”, and
“code!”.
4 Deliverables
Turn in the following files on Gradescope:
1. summation.asm
2. buildMinArray.asm
3. binaryStringToInt.asm
4. fiveCharacterStrings.asm
Note: Please do not wait until the last minute to run/test your homework. Last minute turn-ins will result in long queue times for grading on Gradescope. You have been warned.
5 Running the Autograder and Debugging LC-3 Assembly
1. First off, we can get these replay strings in two places: the local grader, or off of Gradescope. To run the local grader:
• Mac/Linux Users:
(a) Navigate to the directory your homework is in (in your terminal on your host machine, not in the Docker container via your browser)
(b) Run the command sudo chmod +x grade.sh
(c) Now run ./grade.sh
• Windows Users:
(a) In Git Bash (or Docker Quickstart Terminal for legacy Docker installations), navigate to the directory your homework is in
(b) Run chmod +x grade.sh
(c) Run ./grade.sh
When you run the script, you should see an output like this:

Copy the string, starting with the leading ’B’ and ending with the final backslash. Do not include the quotation marks.

2. Secondly, navigate to the clipboard in your Docker image and paste in the string.

3. Next, go to the Test Tab and click Setup Replay String

4. Now, paste your tester string in the box!

5. Now, Complx is set up with the test that you failed! The nicest part of Complx is the ability to step through each instruction and see how they change register values. To do so, click the step button. To change the number representation of the registers, double click inside the register box.

6. If you are interested in looking how your code changes different portions of memory, click the view tab and indicate ’New View’

7. Now in your new view, go to the area of memory where your data is stored by CTRL+G and insert the address

8. One final tip: to automatically shrink your view down to only those parts of memory that you care about (instructions and data), you can use View Tab → Hide Addresses → Show Only Code/Data.

6 Appendix
6.1 Appendix A: ASCII Table

Figure 1: ASCII Table — Very Cool and Useful!
6.2 Appendix B: LC-3 Instruction Set Architecture

6.3 Appendix C: LC-3 Assembly Programming Requirements and Tips
1. Your code must assemble with NO WARNINGS OR ERRORS. To assemble your program, open the file with Complx. It will complain if there are any issues. If your code does not assemble you WILL get a zero for that file.
2. Comment your code! This is especially important in assembly, because it’s much harder to interpret what is happening later, and you’ll be glad you left yourself notes on what certain instructions are contributing to the code. Comment things like what registers are being used for and what less intuitive lines of code are actually doing. To comment code in LC-3 assembly just type a semicolon (;), and the rest of that line will be a comment.
3. Avoid stating the obvious in your comments, it doesn’t help in understanding what the code is doing.
Good Comment
ADD R3, R3, -1 ; counter–
BRp LOOP
Bad Comment ; if counter == 0 don’t loop again
ADD R3, R3, -1 ; Decrement R3
BRp LOOP ; Branch to LOOP if positive
4. DO NOT assume that ANYTHING in the LC-3 is already zero. Treat the machine as if your program was loaded into a machine with random values stored in the memory and register file.
5. Following from 3, you can load the file with randomized memory by selecting “File” ¿ “Advanced Load” and selecting randomized registers/memory.
6. Do NOT execute any data as if it were an instruction (meaning you should put .fills after HALT or
RET).
7. Do not add any comments beginning with @plugin or change any comments of this kind.
8. Test your assembly. Don’t just assume it works and turn it in.
7 Rules and Regulations
7.1 General Rules
2. If you find any problems with the assignment it would be greatly appreciated if you reported them to the author (which can be found at the top of the assignment). Announcements will be posted if the assignment changes.
7.2 Submission Conventions
1. Do not submit links to files. The autograder does not understand it, and we will not manually grade assignments submitted this way as it is easy to change the files after the submission period ends. You must submit all files listed in the Deliverables section individually to Gradescope as separate files.
7.3 Submission Guidelines
2. You are also responsible for ensuring that what you turned in is what you meant to turn in. After submitting you should be sure to download your submission into a brand new folder and test if it works. No excuses if you submit the wrong files, what you turn in is what we grade. In addition, your assignment must be turned in via Canvas/Gradescope. Under no circumstances whatsoever we will accept any email submission of an assignment. Note: if you were granted an extension you will still turn in the assignment over Canvas/Gradescope.
7.4 Syllabus Excerpt on Academic Misconduct
Academic misconduct is taken very seriously in this class. Quizzes, timed labs and the final examination are individual work.
Homework assignments are collaborative, In addition many if not all homework assignments will be evaluated via demo or code review. During this evaluation, you will be expected to be able to explain every aspect of your submission. Homework assignments will also be examined using computer programs to find evidence of unauthorized collaboration.
You are expressly forbidden to supply a copy of your homework to another student via electronic means. This includes simply e-mailing it to them so they can look at it. If you supply an electronic copy of your homework to another student and they are charged with copying, you will also be charged. This includes storing your code on any site which would allow other parties to obtain your code such as but not limited to public repositories (Github), pastebin, etc. If you would like to use version control, use github.gatech.edu
7.5 Is collaboration allowed?

Reviews

There are no reviews yet.

Be the first to review “CS 2110 Homework 5 Intro to Assembly Solved”

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