0
global:
int alpha[8] = { 0xb1, 0xe1, 0x91, 0xc1, 0x81, 0xa1, 0xf1, 0xd1 }; 
int beta[8] = { 0x0, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70 };

main:
int j = 0;
int k = 7;

  loopTop1: //converted while loop to Goto-C
  if ( j == 8) goto loopEnd2;
  alpha[j] = beta[k];
  j++;
  k--;
  goto loopTop1;

  loopEnd2:
return 0;

I'm trying to convert the following code snippet to assembly. This is what I have so far:

    .data
    .globl alpha
alpha:  .word 0xb1, 0xe1, 0x91, 0xc1, 0x81, 0xa1, 0xf1, 0xd1
    .globl beta
beta:   .word 0x0, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70


main:

    la  $s6, beta       # x = beta
    li  $s3, 0          # j = 0
    li  $s4, 7          # k = 7
    
loopTop1:
    beq     $s3, 8, loopEnd1

    addi    $s3, $s3, 1     # j++
    sub     $s4, $s4, 1     # k--
    
    j   loopTop1
    
loopEnd1:
\\end

I have to do this without using pointer arithmetic. The instructions are: " The translation of the while loop should include generation of the address of alpha[j] by adding 4 times j to the address of alpha[j], and a similar thing for beta[k]." I'm having trouble storing the address of beta[k], storing the value of it into a register, and then storing the value of the register into alpha[j].

4
  • The process it describes for finding the address of alpha[j] is pointer arithmetic. I suppose they mean you can do what an optimizing C compiler would do and strength-reduce by turning that shift/add every iteration into just an add reg, reg, 4 pointer-increment. Commented Jan 27, 2021 at 6:49
  • But anyway, what trouble? What have you tried? There's no attempt at the part you're asking about. You could of course ask a C compiler to make an example for you. godbolt.org has MIPS GCC. Also note that "store" generally implies reg -> memory. You want to load from the address beta + k, then store that value into memory at alpha + j. (Where those are C expressions equivalent to &beta[k] and so on, so I left out the scaling by type-width that you need in asm.) Commented Jan 27, 2021 at 6:51
  • @PeterCordes I deleted my work since it wasn't compiling but yeah I wanted to use lw $s1, (4*k)($s5) but this gives a compilation error. I want to load from beta+4k so how would I go about doing this? I tried storing the value of 4*k in $t0 and then tried lw $s1, $t0($s5) where s5 holds the address of beta Commented Jan 27, 2021 at 6:59
  • What addressing modes does MIPS support? Just one, constant(reg). So clearly you have to do the pointer math with other instructions. That wording in your assignment is misleading. Commented Jan 27, 2021 at 7:43

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.