简体   繁体   中英

MIPS assembly code--shifting left using an 8-bit shift count in memory

This is a problem for homework but I'm thoroughly stumped. These problems should have a straight-forward solution, and I'm wondering if the teacher may have mistakenly made this problem like this. But here is the exact text:

"Write a program called LSHIFT to shift logically the 16-bit contents of memory locations 0x10010010 and 0x10010011 left according to the 8-bit shift count stored in memory location 0x10010012."

Here is my problem. To logically shift bits, MIPS only has two instructions: sll, which takes an immediate value (therefore I can't use it, right?) and sllv, which take a value stored in a register, but uses only 5 bits. This means that using sllv, I can only shift the bits a maximum of 32 places (2^5) but the problem wants me to write a program that will shift up to 256 places (2^8 for 8 bit shift count). I can only think of two ways around this:

1) Use multiplication instead

2) Break the 8-bit number into 8 pieces and run 8 separate instructions (eg if the shift count is 256, shift left 32, 8 times).

I also want to double-check--bits can be shifted into other memory locations, correct? Or are they restricted to their 32-bit, %4 memory addresses? As an example, will a 5-bit shift of count 11111 do the same thing as an 8-bit shift of count 11111111 because the bits are restricted to the same 32 bits of memory space?

Correct me if I'm wrong on anything, because like I said, there should be a simple solution.

sllv uses 5 bits to select one of the 32 registers. From there you are not limited to 5 bits of that register.

Take a look at this:

.text
main:

    addi $t0 $zero 1
    addi $t1 $zero 30
    sllv $a0 $t0 $t1

    li $v0 1
    syscall

    jr $ra

Outputs 1073741824 or 2^30.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM