这是一些我相信会起作用的代码:
# 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 版本来提供诊断/参考实现,因为...
没有冒犯,但是,伪代码是一些 [“古代” :-)] 语言来源,例如 Algol、Pascal、Ada、VHDL(?) 或 [现代] Fortran,我在弄清楚算法时遇到了一些困难/意图。
翻译成 C 和直接翻译成 asm 一样容易出错。