Last active
January 14, 2016 17:55
-
-
Save TobleMiner/f94a5e0d243208105de5 to your computer and use it in GitHub Desktop.
DLX asm ASCII upper case to lower case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Registers | |
// R1: Length of text and decrementing counter (Assignment doesn't indicate that it must be preseved) | |
// R2: Temp register | |
// R3: Magic value used to add 32 to all bytes in a word | |
// Memory | |
// 1000..1000 + r1 : ASCII input | |
// 2000..2000 + r1 : ASCII output | |
START: | |
addi r3, r0, #8224 // Construct magic value with bit 6 and bit 14 set | |
slli r3, r3, #16 // Shift magic value to upper 16 bit | |
addi r3, r3, #8224 // Set bit 6 and bit 14 in lower 16 bit again | |
// This creates a bitmask that when added to a | |
// word adds 32 to each byte in the word. 32 is | |
// the offset between 'A' and 'a' in ASCII | |
subi r1, r1, #1 // Decrement length to compensate for offset by 1 of | |
// last source byte in memory vs 1000 + r1 | |
LOOP: | |
slei r2, r1, #-1 // Check if we are done | |
bnez r2, END // Jump to END if we are | |
lw r2, 1000(r1) // Load word/4 bytes/ascii characters from memory | |
add r2, r2, r3 // Add magic value to chars | |
sw 2000(r1), r2 // Store modified chars in target memory area | |
subi r1, r1, #4 // Subtract length of word = 4 bytes from loop counter | |
j LOOP // aaaaand repeat ;) | |
END: | |
halt // Halt when finished | |
// Some additional thoughts: | |
// This program adds 32 to all bytes in a word even if r1 indicates that the | |
// text doesn't fully fill the last word in occupies in memory. I guess that | |
// this isn't a problem here because a) The length of valid data is known and | |
// b) This is a 32 bit CPU with 32 bit word size. This means that even when | |
// working with dynamically allocated memory the memory size would be a multiple | |
// of 4 bytes. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment