Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m only able to use http://homepage.smc.edu/stahl_howard/cs17/FileManager/refe

ID: 3664606 • Letter: I

Question

I'm only able to use http://homepage.smc.edu/stahl_howard/cs17/FileManager/referenceguides/referenceguidev.htm

PROGRAM 7: Recursive Subtraction Function
Write an HLA Assembly language program that implements the following function:

procedure recursiveSubtraction( a: int32; b : int32 ); @nodisplay; @noframe;

This function should return into EAX the value of a - b based on a recursive approach. For simplicity sake, let's assume that both a and b will be greater than or equal to zero. By using recursion, you will be forced to manipulate the runtime stack. Base your solution on the following formulas:

recursiveSubtraction( a, b ) = a when b == 0;
recursiveSubtraction( a, b ) = -b when a == 0;
otherwise, recursiveSubtraction( a, b ) = recursiveSubtraction( a-1, b-1 );

(Hint #1: Because of the nature of static variables, you should hold any partial computed results inside a register, rather than a variable)
(Hint #2: Written in C, the recursiveMultiplicationfunction would have the form:

int recursiveSubtraction( int a, int b )
{
int result = 0;
if (b == 0)
{

result = a;

}
else if (a == 0)
{

result = -b;

}
else
{

result = recursiveSubtraction( a-1, b-1 );

}
return( result );}

(Hint #3: Convert the C function shown above into HLA for this assignment.)
(Hint #4: The instruction NEG can be used to invert the sign of an integer, as in -b).

Shown below is a sample program dialogue.

Feed Me A: 5
Feed Me B: 3
RecursiveSubtraction of A-B = 2

Feed Me A: 10
Feed Me B: 2
RecursiveSubtraction of A-B = 8

Feed Me A: 2
Feed Me B: 10
RecursiveSubtraction of A-B = -8

Explanation / Answer

program MainSubtractionFunction; #include( "stdlib.hhf" ); static iDataValue1 : int32 := 0; iDataValue2 : int32 := 0; DifferenceInt : int32 :=69; procedure recursiveSubtraction( a: int32; b : int32 ); @nodisplay; @noframe; static returnAddress : dword; value: int32; begin recursiveSubtraction; pop( returnAddress ); pop( b ); pop( a ); push( returnAddress ); mov (a, EAX); mov (b, EBX); CompareB: cmp (EBX, 0); je ExitSequence; CompareA: cmp (EAX, 0); je AEqualsZero; NeitherEqualZero: sub (1, EAX); sub (1, EBX); push(EAX); push(EBX); call recursiveSubtraction; AEqualsZero: neg (EBX); mov (EBX, EAX); jmp ExitSequence; BEqualsZero: jmp ExitSequence; ExitSequence: ret(); end recursiveSubtraction; begin MainSubtractionFunction; stdout.put( "Feed Me A: " ); stdin.get( iDataValue1 ); stdout.put( "Feed Me B: " ); stdin.get( iDataValue2 ); push( iDataValue1 ); push( iDataValue2 ); call recursiveSubtraction; mov(EAX, DifferenceInt); stdout.put("RecursiveSubtraction of A-B = ",DifferenceInt, nl); stdout.put("EAX = ",EAX, nl); stdout.put("EBX = ",EBX, nl); stdout.put("ECX = ",ECX, nl); end MainSubtractionFunction;