Search code examples
binarysimulatorlc3

LC-3 binary instructions exercise


This is the code for the LC3 simulator that i have right now:

0011000000000000 0101010010100000 0010011000010000 1111000000100011 0110001011000000 0001100001111100 0000010000001000 1001001001111111 0001001001100001 0001001001000000 0000101000000001 0001010010100001 0001011011100001 0110001011000000 0000111111110110 0010000000000100 0001000000000010 1111000000100001 1111000000100101 0011000100000000 0000000000110000

It's a program that identitfies a single character in an entire string and then outputs the number of times that character is found..

I have 2 questions..

1) The code works so that it outputs the number of times i find a certain letter in a string... But I need to modify it so that it builds a list in the memory of the addresses where the character is found.. I need to make this list start at memory location x3200

2) The code only works if the character is found between 0-9 times.. I need to modify it so that it works from 0-99 times

I'm not asking for the answer... I would just appreciate any pointers on how to approach this... Thanks


Solution

  • Alright so I think I'm starting to understand this problem a little bit more. I created a binary file and then loaded that into the LC3's simulator to produce some readable asm. Here's what I've got:

    Memory:
     x3000  0101010010100000  x54A0  AND    R2, R2, #0     // Clear R2
     x3001  0010011000010000  x2610  LD     R3, x3012      // load the the value stored at x3012 into R3
     x3002  1111000000100011  xF023  TRAP   IN             // input char
     x3003  0110001011000000  x62C0  LDR    R1, R3, #0     // load the value of memory R3 into R1
     x3004  0001100001111100  x187C  ADD    R4, R1, #-4    // Add -4 to R1 to see if we have reached the end of our string
     x3005  0000010000001000  x0408  BRZ    x300E          // Output result, end program if we run into an x0004 in the string
     x3006  1001001001111111  x927F  NOT    R1, R1         // invert the char from the 
     x3007  0001001001100001  x1261  ADD    R1, R1, #1     // stored string
     x3008  0001001001000000  x1240  ADD    R1, R1, R0     // compare with the char entered by the user
     x3009  0000101000000001  x0A01  BRNP   x300B          // If the chars don't match grab another char from the string
     x300A  0001010010100001  x14A1  ADD    R2, R2, #1     // R2 is our char counter, counts the number of times we find
                                                           // the user's char in the string
     x300B  0001011011100001  x16E1  ADD    R3, R3, #1     // increment our memory pointer
     x300C  0110001011000000  x62C0  LDR    R1, R3, #0     // load the value of memory R3 into R1
     x300D  0000111111110110  x0FF6  BRNZP  x3004          // Jump to      x3004
     x300E  0010000000000100  x2004  LD     R0, x3013      // Load the value of      x30 into R0
     x300F  0001000000000010  x1002  ADD    R0, R0, R2     // Add      x30 to our count to convert it to ASCII
     x3010  1111000000100001  xF021  TRAP   OUT            // Output the count to the user
     x3011  1111000000100101  xF025  TRAP   HALT           // Stop the program
     x3012  0011000100000000  x3100  ST     R0, x2F13      
     x3013  0000000000110000  x0030  NOP
    

    To help you get started with your first question, I would do something similar to how R3 is being used. Load the memory location x3200 into an unused register and then increment it each time you store an address in that memory location. Example:

    LD R5, x3013      // store x3200 in the memory location x3013
    .
    .                 // if the chars match then do the following
    .
    STR R3, R5, #0    // Store the value of R3 into the mem of R5
    ADD R5, R5, #1    // increment R5
    

    As for your second question, the reason why it only outputs values 0-9 is because you're converting the count value to an ASCII char by adding x30. This is great for the first 10 integers, but you'll have to get a little creative to add a second digit.

    Hope this helps.