[英]Recursive Division Assembly program
I am working on an assembly, techincally HLA (High Level Assembly), program.我正在开发一个程序集,技术上是 HLA(高级程序集)程序。 I need to convert this C code to assemvbly.我需要将此 C 代码转换为汇编。 Here is the assignment.这是任务。 Write an HLA Assembly language program that implements the following function:编写一个 HLA 汇编语言程序,实现以下功能:
procedure recursiveModulo( a: int32; b : int32 );过程 recursiveModulo( a: int32; b : int32 ); @nodisplay; @nodisplay; @noframe; @无框;
This function should return into EAX the value of a % b based on a recursive approach.这个函数应该基于递归方法将 a % b 的值返回到 EAX 中。 For simplicity sake, let's assume that both a and b will be greater than or equal to zero.为简单起见,我们假设 a 和 b 都大于或等于 0。 By using recursion, you will be forced to manipulate the runtime stack.通过使用递归,您将被迫操作运行时堆栈。 Base your solution on the following formulas:您的解决方案基于以下公式:
Here is the provided C code:这是提供的 C 代码:
int recursiveModulo( int a, int b )
{
int result = 0;
if (a == 0 || b == 0)
{
result = 0;
}
else if (b == 1)
{
result = 0;
}
else if (a < b)
{
result = a;
}
else
{
result = recursiveModulo( a-b, b );
}
return( result );
}
Here is my HLA code:这是我的 HLA 代码:
program RecursiveDivision;
#include( "stdlib.hhf" );
static
iDataValue1 : int32 := 0;
iDataValue2 : int32 := 0;
procedure recursiveModulo( a: int32; b : int32 ); @nodisplay; @noframe;
static
returnAddress : dword;
begin recursiveModulo;
pop(returnAddress);
pop(a);
pop(b);
push(returnAddress);
push(ECX);
push(EBX);
cmp(a, 0);
je equal;
jmp checkb;
checkb:
cmp(b, 0);
je equal;
jmp b1;
b1:
cmp(b, 1);
je equal;
jmp alessb;
equal:
mov(0, EAX);
jmp ExitSequence;
alessb:
mov(a, EBX);
cmp(b, EBX);
jg resulta;
jmp recursive;
resulta:
mov(a, EAX);
jmp ExitSequence;
recursive:
mov(a, ECX);
sub(b, ECX);
push(ECX);
push(b);
call recursiveModulo;
jmp ExitSequence;
ExitSequence:
pop(ECX);
pop(EBX);
ret();
end recursiveModulo;
begin RecursiveDivision;
stdout.put("Give me A",nl);
stdin.get(iDataValue1);
stdout.put("Give me B",nl);
stdin.get(iDataValue2);
push(iDataValue1);
push(iDataValue2);
call recursiveModulo;
stdout.puti32(EAX);
end RecursiveDivision;
So the part of my code that works correctly is the first if block.所以我的代码中正确运行的部分是第一个 if 块。 For the second if block where result should be zero if b = 1, is that it just simply returns 1 instead of 0. For the 3rd conditional where if a is less than b I get this weird issues where it works for some combinations of numbers, but other time it just returns zero.对于如果 b = 1 结果应该为零的第二个 if 块,它只是简单地返回 1 而不是 0。对于第三个条件,如果 a 小于 b 我会遇到这个奇怪的问题,它适用于某些数字组合,但其他时候它只返回零。 for the else block where it is supposed to call the function recursively it just simply returns parameter a.对于应该递归调用函数的 else 块,它只是简单地返回参数 a。 ` `
POP
pops the last pushed value. POP
弹出最后推送的值。 You push first a
then b
, so you have to pop first b
then a
.你先推送a
然后b
,所以你必须先弹出b
然后a
。
Change改变
pop(a);
pop(b);
to到
pop(b);
pop(a);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.