-3

任何知道如何将此伪代码转换为MIPS程序集的人?种子是一个全局变量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; 

回答

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版本提供的诊断/参考实现,因为......

没有违法,但是,伪码是一些[“古” :-)]语言来源,如Algol,Pascal,Ada,VHDL(?)或[现代] Fortran,我在计算算法/意图时遇到了一些困难。

C的翻译本来就像直接翻译为asm一样容易出错。