MIPS的补充&排列

时间:2016-08-13 13:19:16

标签: mips

我需要构建一个MIPS程序,它获取一个特定的数组并打印unsigned(使用两个补码)数组。例如,如果我得到数组23,-2,45,67,89,12,-100,0,120,6 我会收到23,254,45,67,89,12,156,0,120,6。但我不知道要使用哪些功能。 我到目前为止构建的内容:

    .data
array:      .word       23,-2,45,67,89,12,-100,0,120,6
arrend: 

comma: .asciiz ", "

    # array = {23,-2,45,67,89,12,-100,0,120,6}
    # Algorithm being implemented to find the unsign value of array's elements
    # loop i = 0 to length-1 do (use $t1 for i)
    # absolute value = |array[i]|
    # end loop (use $t3 for base addr. of array)

    # registers:
    #   t3 -- pointer to current array element (e.g. arrptr)
    #   t2 -- pointer to end of array
    # 
    #   t4 -- current value fetched from array (i)

    .text

main:

    la      $t3,array               # load base addr. of array
    la      $t2,arrend              # load address of array end
    j       test 

loop:
    lw      $t4,0($t3)              # load array[i]
    addi    $t3,$t3,4               # increment array pointer

    #######

    # print unsign value of array's element
    li      $v0,1
    addi    $a0,$t5,0
    syscall

    # print comma
    li      $v0,4
    la      $a0,comma
    syscall

test:
    blt     $t3,$t2,loop            # more to do? if yes, loop

其中#######应该是在unsign元素中转换数组元素的代码。我该怎么写呢?

1 个答案:

答案 0 :(得分:0)

To get the absolute value, you need to do some branching logic. Figure out if it's negative. If so, subtract it from zero.

slt $t5, $t4, $zero
beq $t5, $zero, continue
sub $t4, $zero, $t4
continue:

However, if you want to get the unsigned value of a signed byte, try this instead.

sll $t4, $t4, 24
srl $t4, $t4, 24