Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

LC-3 ASSEMBLY HELP! You will complete an LC-3 assembly language program that implements a number of subroutines. Several of these subroutines are duplicates of C

LC-3 ASSEMBLY HELP!

You will complete an LC-3 assembly language program that implements a number of subroutines. Several of these subroutines are duplicates of C functions for strings. Subroutine parameters will be passed in register(s) R0/R1 and results will be returned in register(s). You may find that you do not have enough registers to keep all of your values in registers. In this case, you can create labels to hold them.

IMPORTANT:

  • Don't use registers R5 and R6 in this program (not even in comments). You may use R7 but be careful about its handling.
  • Don't change the reserved section of the code. The only exception to this is that you may change the .FILL values for Option, Value1 and Value2. This makes it easy to initialize the values in the program, so that you do not need to continually re-enter them every time you reset the simulator.
  • Never assume that registers are initialized to 0 by the time your subroutine starts. If you want a register to be initialized to 0, you must do it yourself in your code.

The following list is the set of subroutines you must implement.

Part A:

For testing purposes, it's useful to read and understand what each Test_ subroutine does in the reserved section. For example, the Test_packsubroutine loads the values of the Value1 and Value2 labels into R0 and R1 respectively. Then, it calls your pack subroutine. When your subroutine returns, it stores the value of R0 into the Result label. This tells you that to test your pack subroutine, you must enter the parameters in Value1 and Value2, and then check the result in the Result label.

  • pack(b1, b0) - Pack (combine) the lower 8 bits of R0 and the lower 8 bits of R1 and pack them into a single 16 bit quantity in R0. The lower 8 bits of R0 will be in the upper 8 bits of the result. Here is a visual example:

    image text in transcribed

  • unpack(b) - Unpack the 16 bits in R0 into registers R0/R1 such that the upper 8 bits of R0 are stored in the lower 8 bits of R0 and the lower 8 bits of R0 are stored in the lower 8 bits of R1. The rest of R0 and R1 needs to be padded with 0's. Here is a visual example:

    image text in transcribed

  • printCC() - Print the word NEGATIVE, ZERO, POSITIVE depending on the value in register R0. At exit, register R0 must contain the original value. Each word is followed by a newline. Do not depend on the condition code being set to the correct value on entry. The test ofstrcmp() uses this subroutine. Notice that we have provided you with labels referring to the strings that you need to print (StringNEG, StringZERO, and StringPOS). The PUTS trap can be used to print a string value stored in memory to the LC3 console. PUTS prints the string beginning at the memory location contained in R0.

IMPORTANT: after your subroutine completes, control must return to the corresponding Test_ subroutine in the reserved section. For example, when your printCC subroutine gets to the RET, the program needs to return to right after JSR printCC in the Test_printCC subroutine. Otherwise, it means that you are not handling R7 correctly. This applies to both part A and part B.

Part B:

  • strlen(s) - On entry R0 contains the address of a valid (null terminated) C string. On exit, R0 contains the length. Note that the null terminator is not counted in the length.
  • strcpy(dest, src) - On entry R0 contains a pointer to the start of the destination string (dest), and R1 contains a pointer to the start of the null-terminated source string (src). On exit, the source string has been copied to the destination string (including the null terminator) and R0 contains a pointer to the start of the destination string.
  • strcat(dest, src) - On entry R0 contains a pointer to the start of the destination string (dest), and R1 contains a pointer to the start of the source string (src). On exit, the source string has been appended to the end of the destination string and R0 contains a pointer to the start of the destination string. For example, if "cdf" is to be appended to "abc", the destination string becomes "abccdf" and R0 is a pointer to the "a". The destination string must be null terminated.
  • strcmp(s1, s2) - On entry R0 contains a pointer to the start of a string (s1), and R1 contains a pointer to the start of another string (s2). On exit R0 contains a negative number if s1 s2, based on the lexicographic ordering using ASCII values: e.g., Z is less than a. In lexicographic ordering, we traverse the strings from left to right until the first difference is found. The differring characters tell us which string is greater. For example, abc Do not assume the strings have the same length (or even that the length is greater than 0).

Getting Started

Start with the file string.asm. You may assemble and run the program. For many of the subroutines you will be prompted to enter a string (see the blue "console"). Enter whatever you like (

Testing/debugging each individual subroutines follows a pattern:

  1. Write/modify the code for a single subroutine.
  2. Assemble your program using lc3as.
  3. Load your program into the simulator/debugger.
  4. Set the variable Option to the index of the routine you wish to test. You may want to change the .FILL value so that you do not need to continually reset it as you write, and debug your subroutine.
  5. Set a break point at the entry of your subroutine, or wherever you would like to start your debugging.
  6. Continue the simulator/debugger. If you have set a breakpoint, it will stop when your program reaches that line. Use Step/Next to watch your code execute and examine registers and memory locations for correctness. You will want to use Next so that you do NOT step into traps like GETS or PUTS. You may step into them, but you will find them very tedious to step through.
  7. When the program halts, examine output and/or memory locations for results.
  8. Test your code with multiple test values and verify results.

When you have completed one subroutine, move on to the next one.

You may want to call one subroutine from another, or add subroutines to accomplish specific tasks. Recall that calling a subroutine from another subroutine requires careful handling of the return address (R7), and that registers from the calling routine may be overwritten.

********************************************************************************************************************************************

string.asm

; Begin reserved section: do not change ANYTHING in reserved section! ; The ONLY exception to this is that you MAY change the .FILL values for ; Option, Value1 and Value2. This makes it easy to initialize the values in the ; program, so that you do not need to continually re-enter them. This ; makes debugging easier as you need only change your code and re-assemble. ; Your test value(s) will already be set. ;------------------------------------------------------------------------------ ; Author: Fritz Sieker ; ; Description: Tests the implementation of a simple string library and I/O ; .ORIG x3000 BR Main Functions .FILL Test_pack ; (option 0) .FILL Test_unpack ; (option 1) .FILL Test_printCC ; (option 2) .FILL Test_strlen ; (option 3) .FILL Test_strcpy ; (option 4) .FILL Test_strcat ; (option 5) .FILL Test_strcmp ; (option 6) ; Parameters and return values for all functions Option .FILL 0 ; which function to call String1 .FILL x4000 ; default location of 1st string String2 .FILL x4100 ; default location of 2nd string Result .BLKW 1 ; space to store result Value1 .FILL 0 ; used for testing pack/unpack Value2 .FILL 0 ; used for testing pack/unpack lowerMask .FILL 0x00FF ; mask for lower 8 bits upperMask .FILL 0xFF00 ; mask for upper 8 bits Main LEA R0,Functions ; get base of jump table LD R1,Option ; get option to use, no error checking ADD R0,R0,R1 ; add index of array LDR R0,R0,#0 ; get address to test JMP R0 ; execute test Test_pack LD R0,Value1 ; load first character LD R1,Value2 ; load second character JSR pack ; pack characters ST R0,Result ; save packed result End_pack HALT ; done - examine Result Test_unpack LD R0,Value1 ; value to unpack JSR unpack ; test unpack ST R0,Value1 ; save upper 8 bits ST R1,Value2 ; save lower 8 bits End_unpack HALT ; done - examine Value1 and Value2 Test_printCC LD R0,Value1 ; get the test value .ZERO R1 ; reset condition codes JSR printCC ; print condition code End_printCC HALT ; done - examine output Test_strlen LD R0,String1 ; load string pointer GETS ; get string LD R0,String1 ; load string pointer JSR strlen ; calculate length ST R0,Result ; save result End_strlen HALT ; done - examine memory[Result] Test_strcpy LD R0,String1 ; load string pointer GETS ; get string LD R0,String2 ; R0 is dest LD R1,String1 ; R1 is src JSR strcpy ; copy string PUTS ; print result of strcpy NEWLN ; add newline End_strcpy HALT ; done - examine output Test_strcat LD R0,String1 ; load first pointer GETS ; get first string LD R0,String2 ; load second pointer GETS ; get second string LD R0,String1 ; dest is first string LD R1,String2 ; src is second string JSR strcat ; concatenate string PUTS ; print result of strcat NEWLN ; add newline End_strcat HALT ; done - examine output Test_strcmp LD R0,String1 ; load first pointer GETS ; get first string LD R0,String2 ; load second pointer GETS ; get second string LD R0,String1 ; dest is first string LD R1,String2 ; src is second string JSR strcmp ; compare strings JSR printCC ; print result of strcmp End_strcmp HALT ; done - examine output ;------------------------------------------------------------------------------ ; End of reserved section ;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------ ; on entry R0 contains first value, R1 contains second value ; on exit R0 = (R0   Before Subroutine RO: 0x1234 R1: 0x5678 RO: x3478 After Subroutine Before Subroutine R0: 0x3478 R0: 0x0034 R1: 0x0078 After Subroutine

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2006 Edbt 2006 Workshops Phd Datax Iidb Iiha Icsnw Qlqp Pim Parma And Reactivity On The Web Munich Germany March 2006 Revised Selected Papers Lncs 4254

Authors: Torsten Grust ,Hagen Hopfner ,Arantza Illarramendi ,Stefan Jablonski ,Marco Mesiti ,Sascha Muller ,Paula-Lavinia Patranjan ,Kai-Uwe Sattler ,Myra Spiliopoulou ,Jef Wijsen

2006th Edition

3540467882, 978-3540467885

More Books

Students also viewed these Databases questions

Question

Tell me about yourself.

Answered: 1 week ago

Question

How would we like to see ourselves?

Answered: 1 week ago