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].
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 anadd reg, reg, 4pointer-increment.beta + k, then store that value into memory atalpha + 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.)