[SOLVED] CS2110 -  Homework 5 - Intro to Assembly

30.00 $

Category:

Description

5/5 - (1 vote)

1           Overview

1.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.

1.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. asm
  2. asm
  3. asm
  4. asm

2           Detailed Instructions

2.1         Part 1: GCD

To start you off with this homework, we are implementing the GCD function! Store the result of the operation in the label ANSWER. Arguments A and B are stored in memory, and you will load them from there to perform this operation. Implement your assembly code in gcd.asm.

Suggested Pseudocode:

ANSWER = 0; while (a != b) { if (a > b) { a = a – b;

} else { b = b – a;

}

}

ANSWER = a;

2.2         Part 2: Array Merge

The second assembly function is to implement the “merge” part of merge-sort into an array in memory. You will be merging two pre-sorted input arrays into a third array, so that the third array is also sorted. The third array will have enough space for the final answer. Use the pseudocode to help plan out your assembly and make sure you are sorting it properly! Implement your assembly code in merge.asm.

Suggested Pseudocode:

x = 0; // first index of ARR_X y = 0; // first index of ARR_Y z = 0; // first index of ARR_RES while (x < LENGTH_X && y < LENGTH_Y) { if (ARR_X[x] <= ARR_Y[y]) { ARR_RES[z] = ARR_X[x]; z++: x++;

} else {

ARR_RES[z] = ARR_Y[y]; z++; y++;

}

}

while (x < LENGTH_X) { ARR_RES[z] = ARR_X[x]; z++; x++;

}

while (y < LENGTH_Y) { ARR_RES[z] = ARR_Y[y]; z++; y++;

}

// the final merged array should be in ARR_RES

2.3         Part 3: Studly Caps

The third assembly function is to turn a null-terminated string into a string with studly caps. Studly caps, also known as alternating caps, sticky caps, or occasionally “SpongeBob case”, consists of alternating the letters in a string between lowercase and uppercase. In this function, the first letter is expected to be lowercase and the subsequent letters alternate between uppercase and lowercase depending on the index. The index starts at 0, if the index is even, the character should be lowercase. If the index is odd, the character is expected to be uppercase.

The label STRING will contain the address of the first character of the string to be converted. Convert the string in-place, so that the result string is also stored at the same label STRING. Remember that strings are just arrays of consecutive characters. You are given four constants to use in your program, LOWERA which is the value of the ASCII character ‘a’, LOWERZ which is the value of ‘z’, UPPERA which is the value of ‘A’, and UPPERZ which is the value of ‘Z’. Implement your assembly code in studlyCaps.asm

Assume that the strings are random: they can contain characters, numbers, spaces and symbols.

To convert a character between lowercase and uppercase, refer to the ASCII table and remember that each of these characters are represented by a word (16-bits) in the LC-3’s memory. This is a null-terminated string, meaning that a 0 will be stored immediately after the final character in memory!

NOTE:

  • 0 is the same as ‘\0’
  • 0 is different from ‘0’

Suggested Pseudocode:

string = “TWENTY 1 ten” i = 0;

while (string[i] != 0) { if (i % 2 == 0) {

// should be lowercase if (’A’ <= string[i] <= ’Z’) { string[i] = string[i] | 32;

}

} else {

// should be uppercase if (’a’ <= string[i] <= ’z’) { string[i] = string[i] & ~32;

} } i++;

}

The result here is tWeNtY 1 TeN.

2.4         Part 4: Palindromes

For the final problem, your goal is to determine whether a null-terminated string is a palindrome. A palindrome is a word, phrase or a sequence of numbers that reads the same backward as forward. For instance, “racecar” is a palindrome because it is “racecar” when read both backward and forward.

The label STRING will contain the address of the first character of the string to be checked. Remember that this string is null-terminated. The result will be stored at the label RESULT. If the given string is a palindrome, the result should be set to 1. Else, it should be set to 0. Implement your assembly code in palindrome.asm

Assume every alphabetic character in the string is in lowercase. The string can contain numbers.

NOTE:

  • 0 is the same as ‘\0’
  • 0 is different from ‘0’

Suggested Pseudocode:

string = “racecar”; len = 0;

// to find the length of the string while (string[len] != ’\0’) { len = len + 1;

}

// to check whether the string is a palindrome result = 1; i = 0; while (i < length) { if (string[i] != string[length – i – 1]) { result = 0; break;

}

i = i + 1;

}           Appendix       Appendix A: ASCII Table

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

        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