1

我写了这段代码,但我的教授一直告诉我要保存和恢复我的寄存器。我以为我是通过向空寄存器声明一个值。

program middleFinder;
#include( "stdlib.hhf" ); // imports the input and output library
static
iDataX : int16 := 0;
iDataY : int16 := 0;
iDataZ : int16 := 0;


procedure middleFindernow( x: int16; y : int16; z : int16 ); @nodisplay; @noframe; 
// this caller must free up DH
static
iReturnAddress : dword;
iMid : int16;
iTemporary : int16;
iRegisterValue : dword;


begin middleFindernow;


mov( EBX, iRegisterValue );
// acquire parameters on the stack
pop( iReturnAddress );
pop(iTemporary);
pop(iTemporary);
mov(iTemporary, BX);
mov( BX, z );
pop( iTemporary ); // this is Y
mov( iTemporary, BX );
mov( BX, y );
pop( iTemporary ); // this is X
mov( iTemporary, BX );
mov( BX, x );
// push back the return
push( iReturnAddress );
push( iRegisterValue );

// calculate x - y
mov(x, DX);
cmp(y, DX);
je XYequal;
cmp(y, DX);
jl YisMax;
cmp(y, DX);
jg XisMax;

XYequal:
mov(y, BX);
cmp(z, BX);
je equal;
jmp ExitSequence;

equal:
stdout.put("All three values are equal");
stdout.put("AL=1");
jmp ExitSequence;

XisMax:
// calculate x - z
mov(0,BX);
mov(z,BX);
cmp(x,BX);
jl PutXinDX;
jg PutZinDX;


YisMax:
// calculate y - z
mov(0, BX);
mov(y, BX);
cmp(z, BX); 
jl PutYinDX;
jg PutZinDX;


PutXinDX:
mov(x, DX);
jmp ExitSequence;

PutYinDX:
mov(y, DX);
jmp ExitSequence;

PutZinDX:
mov(z, DX);
jmp ExitSequence;

ExitSequence:
// exit sequence
// pop all preserved registers --- BX
pop( EBX );
ret();


end middleFindernow;
begin middleFinder;
stdout.put( "gimme a X value: " );
stdin.get( iDataX);
stdout.put( "gimme a Y value: " );
stdin.get( iDataY );
stdout.put( "gimme a Z value: " );
stdin.get( iDataZ ); 
stdout.put( "Calling Mid number " , nl );
mov( 0, BX );
mov( iDataX, BX );
push( BX );
mov( 0, BX );
mov( iDataY, BX );
push( BX );
mov( 0, BX );
mov( iDataZ, BX );
push( BX );
mov( 0, BX );
push( BX );
call middleFindernow;
stdout.put( "The value in the middle is " );
stdout.puti16( DX );
stdout.newln();

end middleFinder;
4

2 回答 2

0

我想教授想要,在您的函数开始时,您将推送所有寄存器,该函数正在更改为堆栈,并且在返回之前您将恢复这些寄存器的实际值。这是必需的,因为将调用您的函数的代码稍后可能会使用这些值,并且不希望这些值被更改。

于 2017-08-13T08:51:44.630 回答
0

您使用了一些像 BX 这样的寄存器,但最后您没有从程序开始之前恢复它们的初始值,这可能是您教授的意思

于 2017-08-13T06:15:35.177 回答