-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一樣容易出錯。