1

I am trying to print binary digits of any integer input and store them in the array starting at the last index. Then I am trying to print it from the array.

    .data

prompt: .asciiz "Enter an int: "
errorLarge: .asciiz "Error Value to large CANNOT be held in 16 bit"
errorSmall: .asciiz "Error Value is to small CANNOT be held in 16 bits"

# 64bytes =512 bits created (1 int =4 bytes):: (16 int =64 bytes)
array: .space 64

newLine: .asciiz "\n"

    .globl main
    .text

main:
    li $v0,4
    la $a0,prompt
    syscall

    li $v0,5
    syscall
    move $t0,$v0

    li $t1,32767
    li $t2,-32767

    bgt $t0,$t1,InputToGreat
    blt $t0,$t2,InputToSmall

    li $t2,2
    li $t5,64     # last memory location in array+1

    li $t7,0

    j initializer

InputToGreat:

    li $v0,4
    la $a0,errorLarge
    syscall

    j main

InputToSmall:

    li $v0,4
    la $a0,errorSmall
    syscall

    j main

finalizer:

    subi $t5,$t5,4
    sw  $t4,array($t5)

    li $t4,0

    bne $t5,$zero, finalizer

OutPut:

    lw $t6,array($t7)

    li $v0,1
    move $a0,$t6
    syscall

    addi $t7,$t7,4
    bne  $t7,252,OutPut

    li $v0,10
    syscall

initializer:

    div    $t0,$t2  # (inside house) 1) 12/2  2) 6/2   3) 3/2
    mflo   $t0  #quotient       6        3        1
    mfhi   $t4  #rem                0        0        1

    beq    $t4,1,finalizer

InputToArray:

    subi $t5,$t5,4
    sw  $t4,array($t5) #first time array+60 last location in array

    li $v0,1
    move $a0,$t4
    syscall

    j initializer

I am getting an error on line 99 sw $t4,array($t5) #first time array+60 last location in array which says

line 99: Runtime exception at 0x004000d8: store address not aligned on word boundary 0x100100ab

nalzok
  • 14,965
  • 21
  • 72
  • 139
Rahul
  • 19
  • 2
  • I am getting an error on line 99 sw $t4,array($t5) #first time array+60 last location in array which says line 99: Runtime exception at 0x004000d8: store address not aligned on word boundary 0x100100ab – Rahul Jul 18 '17 at 18:26
  • I'm not a MIPS programmer, but I suspect that you just need a `.align` (may not be the exact wording) directive before the definition of `array`, to make sure it starts on a 4-byte boundary - those `.asciiz`s before it could end on any alignment. Or, move it to the top of the .data section, where alignment is probably guaranteed. – jasonharper Jul 19 '17 at 16:11

1 Answers1

1

Because you store into array with sw, array must be 4 byte aligned. This a restriction of the mips architecture. Likewise for lw.

So, change:

array: .space 64

Into:

    .align 4
array: .space 64

Also, note that $t5 should be divisible by 4 (which it is when your test program runs)

Craig Estey
  • 30,627
  • 4
  • 24
  • 48
  • Background on `.align` in MARS: [MIPS Assembly Alignment Align n](https://stackoverflow.com/q/41359415). Note that it takes a power-of-2 argument, so you only need `.align 2`. See also [MARS MIPS simulator's built-in assembler aligns more than requested?](https://stackoverflow.com/q/59926448) for more details about exactly how it works. Note that `.word` has implicit alignment to a word boundary (unless you disable it with `.align 0`), but `.space` doesn't. – Peter Cordes Mar 21 '20 at 02:59