MIPS汇编中的代码生成器

时间:2016-09-20 20:18:59

标签: assembly mips code-generation unsigned-integer signed-integer

任何知道如何将此伪代码转换为MIPS汇编的人?种子是一个全局变量

FUNCTION codgen(): UNSIGNED INTEGER;
LOCAL SIGNED INTERGER n;
LOCAL UNSIGNED INTEGER x,y;
BEGIN
n:= [right-justify the five bits "seed"<24:20>, and zero-extend];
WHILE (n >= 0) LOOP
x := [shift "seed" left-logical by 3 bits];
y := [divide "seed" (unsigned) by the constant 16];
seed:= x-y; [ignore overflow condition]
n := n-1;
ENDLOOP
RETURN( seed XOR 0x0176f7df);
END;

1 个答案:

答案 0 :(得分:0)

以下是我认为可行的一些代码:

# codgen -- generate code or random number
#
#@+
# FUNCTION codgen(): UNSIGNED INTEGER;
# LOCAL SIGNED INTEGER n;
# LOCAL UNSIGNED INTEGER x,y;
# BEGIN
#   n := [right-justify the five bits "seed"<24:20>, and zero-extend];
#   WHILE (n >= 0) LOOP
#     x := [shift "seed" left-logical by 3 bits];
#     y := [divide "seed" (unsigned) by the constant 16];
#     seed := x-y; [ignore overflow condition]
#     n := n-1;
#   ENDLOOP
#   RETURN (seed XOR 0x0176f7df);
# END;
#@-
#
# arguments:
#   s0 -- seed
#
# NOTE:
#   under mips, the phrase "seed is a global variable" allows it to be in a
#   register. if it had to be a global "in memory" (e.g.):
#     seed: .word 0
#   just add (at start):
#     lw $s0,seed
#   and (at end):
#     sw $s0,seed
#
# registers:
#   t0 -- n
#   t1 -- x
#   t2 -- y
codgen:
    srl     $t0,$s0,20              # get seed bits 24-20 to lower 5 bits
    andi    $t0,$t0,0x1F            # isolate them
    j       codgen_start            # start loop

codgen_loop:
    sll     $t1,$s0,3               # x = seed << 3
    srl     $t2,$s0,4               # y = (unsigned) seed / 16 (aka seed >> 4)
    subu    $s0,$t1,$t2             # seed = x - y

    subi    $t0,$t0,1               # n -= 1
codgen_start:
    bgez    $t0,codgen_loop         # n >= 0? if yes, loop

    xori    $v0,$s0,0x0176F7DF      # ret = seed ^ 0x0176F7DF
    jr      $ra                     # return

以上是只是桌面检查。我做了实际运行它并且首先编写C版本以提供诊断/参考实现,因为......

没有冒犯,但是,伪代码是某些[&#34;古代&#34; :-)]语言来源如Algol,Pascal,Ada,VHDL(?)或[现代] Fortran,我在确定算法/意图时遇到了一些困难。

对C的翻译与直接翻译为asm一样容易出错。