0

So on my assignment, I have to process a string typed from console and remove CONTROL characters like (!@#$%^&*...), I know it can be done with sb/lb, but our professor wants us to read each character using lw/sw. I don't want the answer but how in heavens do you do such a thing??? I figured out a way but the memory is the problem what memory alignment should I have to my arrays, where I save the characters.

I think the solution has to do with sll and srl BUT the saving the characters is the problem.

MOOSE
  • 1
  • 1
  • 2
    Yes shifting and masking. For storing a character first align the address and load the word. Then mask out the byte you are going to modify. Shift the character into position and put it into the word. Finally write the word back to memory. – Jester Dec 13 '22 at 13:02
  • LW/modify/SW of the containing word (`ptr &= -4`) isn't thread-safe, which is one reason for CPUs to provide byte loads/stores, but it can be done. (And was, [on DEC Alpha](https://stackoverflow.com/questions/46721075/can-modern) which didn't have an `lb` / `sb`). Ideally you can use SWAR (SIMD-within-a-register) techniques to operate on 4 bytes at once with bithacks to suppress carry-out from subtraction, or at least unpack to 4 separate registers then recombine, instead of actually loading/storing the same word 4 times, except at the end of a loop when there's an odd number left. – Peter Cordes Dec 13 '22 at 17:23
  • If you are free to do anything, one approach is to store each character in one word, in effect using too much storage, or what we might refer to as an *unpacked array*. If you control the location of storage, and the use of the storage, you can do this: you would read each character using `lbu`, and then write them one-by-one to a word array, so later can access this other array using `lw`/`sw`. – Erik Eidt Dec 13 '22 at 19:32
  • However, given the wording "string typed from console", which would create an ordinary string as array of bytes, and "read each character using `lw`", I find it likely that your instructors want you to be able to access any and all bytes (any byte address) of an ordinary string directly (without making an expanded copy), using lw/sw. – Erik Eidt Dec 13 '22 at 19:34
  • I want to thank everyone for taking the time out of their day to give me some feedback. I never expected such a response. I know that the answer lays within bit masking and I know I have to use the bit wise operands or,and but I still don't understand, do I have to make specific masks for each unwanted character? I am not giving up, I will continue trying. – MOOSE Dec 14 '22 at 21:38
  • If you just need to read and write sequentially it's as simple as loading a word then masking off the first byte using `0xff`. Shift the word right by 8 bits. Repeat 4 times then get next word. For the writing start with a zeroed register. Shift the value left by 8 times the byte position, merge it into the the output using `OR`. Write it out after you got 4 bytes. PS: you should align your input and output buffers to word boundaries to make life easier. – Jester Dec 15 '22 at 02:30

0 Answers0