Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly 从程序返回,并在不使用JAL指令的情况下使用$ra寄存器继续循环_Assembly_Mips_Qtspim - Fatal编程技术网

Assembly 从程序返回,并在不使用JAL指令的情况下使用$ra寄存器继续循环

Assembly 从程序返回,并在不使用JAL指令的情况下使用$ra寄存器继续循环,assembly,mips,qtspim,Assembly,Mips,Qtspim,此循环扫描一个字符串,对于每个字符,如果等于该字母中的一个,则转到特定过程: switch: add $t1, $zero, $t2 add $t1, $t1, $s2 # $s2 is the address of a string lb $t0, 0($t1) blt $t0, 0x41, done bgt $t0, 0x45, done beq $t0, 0x41, algoA

此循环扫描一个字符串,对于每个字符,如果等于该字母中的一个,则转到特定过程:

switch:
    add     $t1, $zero, $t2
    add     $t1, $t1, $s2      # $s2 is the address of a string
    lb      $t0, 0($t1)

    blt     $t0, 0x41, done
    bgt     $t0, 0x45, done

    beq     $t0, 0x41, algoA   # These are the jump at the procedure
    beq     $t0, 0x42, algoB   # The parameter in input are setted
    beq     $t0, 0x43, algoC   # Before that SWITCH
    beq     $t0, 0x44, algoD
    beq     $t0, 0x45, algoE

endSwitch:
    add     $t2, $t2, 1
    beq     $t2, $s1, done
    j       switch
done:
对于学校项目,我需要每个“Algo”标签必须是一个过程

首先定义输入参数

我不知道在循环过程中如何使用命令
jr$ra
从过程返回到开关

我在想,也许我必须在
$ra
注册
endSwitch:
标签地址

我不相信这是正确的

下面是伪代码:

while x <= len(string)
    switch string[x]
        case A: algoA(); x++;
        case B: algoB(); x++;
        case C: algoC(); x++;
        case D: algoD(); x++;
        case E: algoE(); x++;
        default: x = len(string);
而x这应该是可行的

# void Sw(const char*);
#   will call:
#     extern void algoA(void), algoB(void),
#       algoC(void), algoD(void), algoE(void);
Sw:
    .set reorder
    addiu   $sp, $sp, -32 # reserve 32 bytes on stack:
                          # 8 unused
                          # 8 for $s0 and $ra
                          # 16 reserved for calls to algoX()
                          # (32 to ensure $sp is properly aligned)
    sw      $s0, 20($sp) # save $s0 on stack
    sw      $ra, 16($sp) # save $ra on stack

    move    $s0, $a0 # algoX() will preserve $sX regs

Sw_loop:
    lbu     $t0, 0($s0) # read a char

    addiu   $t0, $t0, -65 # translate 65(A)...69(E) to 0...4
    sltiu   $t1, $t0, 5 # all others will translate to 5 or more
    beqz    $t1, Sw_done # done if 5 or more

    la      $t1, Sw_table # $t1 = address of Sw_table[]
    sll     $t0, $t0, 2 # multiply 0...4 by 4 to index Sw_table[]
    addu    $t0, $t0, $t1 # $t0 = address into Sw_table[]

    lw      $t0, 0($t0) # $t0 = address of algoX()
    jalr    $t0 # call algoX()

    addiu   $s0, $s0, 1 # advance the address to the next char
    b       Sw_loop # repeat

Sw_done:
    lw      $s0, 20($sp) # restore $s0
    lw      $ra, 16($sp) # restore $ra
    addiu   $sp, $sp, +32 # free stack space
    jr      $ra # return

Sw_table: # table of addresses of algoA()...algoE()
    .word   algoA
    .word   algoB
    .word   algoC
    .word   algoD
    .word   algoE

jr$ra
用于从您使用
jal
跳转到的内容返回,这不是您正在做的。不清楚您正在尝试做什么。我建议先用更高级的语言编写您想要的算法。我已经修改了问题。还有一种可能性:
BLTZAL
(小于零的分支和链接)执行条件函数调用。()(但我不知道如何使用它或
BGEZAL
来模拟EQ条件/模拟“beqal”)。但在本例中,是的,索引函数指针表显然更适合OP的连续开关/案例值。@PeterCordes原则上,您可以执行一系列的
addiu-1+bltzal-algoA+addiu-1+bltzal-algoB+addiu-1+…
,而不是遍历该表。但是,
bltzal
在R6上不可用(除了在它的
nal
表单中;R6提供了一个不同的带链接的条件分支)。以上是普遍的。