COMP 228 Fall 2019 Assignment 4 Solutions Problem 1. Address 0x000 0x001 0x002 0x003 0x004 0x005 0x006 0x007 0x008 0x009 0x00A 0x00B 0x00C Contents 0x5000 0x200D 0x5000 0x200E 0x100D 0x300E 0x6000 0x100D 0x400E 0x6000 0x8400 0x9000 0x7000 Code Input Store Input Store Load Add Output Load Subt Output Skipcond Jump Halt 00D 00E 00D 00E 00D 00E 400 000 The user is prompted to enter two numbers. The sum of the two numbers is printed, and then the difference is printed. Finally, if the difference is equal to 0, then the program stops, if it is not equal to 0, then it loops back to the beginning. Problem 2. Loop, Continue, End, CharArray, One, Underscore, LoadI SkipCond Jump Jump Output Load Output Load Add Store Jump Halt HEX DEC HEX CharArray 400 Continue End Underscore CharArray One CharArray Loop 00F 1 05F // Unicode characters (don't uncomment this or the comments below) HEX 068 // h HEX 065 // e HEX 06C // l HEX 06C // l HEX 06F // o HEX 020 // space HEX 077 // w HEX 06F // o HEX 072 // r HEX 06C // l HEX 064 // d HEX 021 // ! Each instruction and directive holds a space in memory. When we uncomment the 3 lines of code, we are inserting new lines of code which “push down” the memory locations of any instructions or directives below the new code. CharArray is initially 0x00C which is the address containing the first character ‘h’, however, since we added 3 lines of code before it, the location of ‘h’ has been “pushed” to 0x00F. So we can simply change CharArray from 0x00C to 0x00F to get the desired result. However, if we insert or remove lines of code in the future, we’ll have to adjust the value once again. Problem 3. Start, Input Store Input Store Store OuterCount InnerCount InnerCountConst OuterLoop, Load Output X InnerLoop, Load Output x Load Subt Store Skipcond Jump InnerCount One InnerCount 400 InnerLoop Load Store InnerCountConst InnerCount Load Subt Store OuterCount One OuterCount Skipcond Jump 400 OuterLoop End, One, OuterCount, InnerCount, InnerCountConst, X, x, Halt DEC DEC DEC DEC HEX HEX 1 0 0 0 058 078 Problem 3. (JnS Alternative) Start, OuterLoop, Input Store Input Store Store OuterCount InnerCount InnerCountConst Load Output X JnS InnerLoop Load Store InnerCountConst InnerCount Load Subt Store Skipcond Jump OuterCount One OuterCount 400 OuterLoop End, Halt One, OuterCount, InnerCount, InnerCountConst, X, x, DEC DEC DEC DEC HEX HEX 1 0 0 0 058 078 InnerLoop, InnerLoopStart, HEX Load Output Load Subt Store Skipcond Jump JumpI 000 x InnerCount One InnerCount 400 InnerLoopStart InnerLoop Problem 4. Below is a sample solution. There are many possible solutions. So long as all functionality described in the problem is implemented. In this implementation, CharArrayLength keeps track of the number of characters inputted, this way, CharArrayLength can be used to terminate the loop that prints the characters in reverse as well as terminating the program if the user enters # as their first symbol. InputLoop, StoreInCharArray, PrintCharArray, Print, PrintLoop, End, One, #, CharArrayLength, CharArrayPointer, Input Subt Skipcond Jump Jump Add StoreI Load Add Store Load Add Store Jump Load Skipcond Jump Jump Load Subt Store LoadI Output Load Subt Store Load Subt Store Skipcond Jump Halt HEX HEX HEX HEX # 400 StoreInCharArray PrintCharArray # CharArrayPointer CharArrayPointer One CharArrayPointer CharArrayLength One CharArrayLength InputLoop CharArrayLength 400 Print End CharArrayPointer One CharArrayPointer CharArrayPointer CharArrayPointer One CharArrayPointer CharArrayLength One CharArrayLength 400 PrintLoop 001 023 000 024