0

Write an HLA Assembly language program that computes the surface area based on a radius. A sample program dialogue is shown below. However you decide to do it, your main program should include calling a procedure or function (atleast one...) to solve this problem.

I have written my code but get "####" as the output of the surface area heres my code:

program surfaceArea;
#include( "stdlib.hhf" );
static
radius : real32;

procedure computeSurfaceArea(r : real32); @nodisplay; @noframe;
static
returnAddress : dword;
area : real32;

begin computeSurfaceArea;

pop(returnAddress);
pop(r);
push(returnAddress);

finit();
fld( r );
fld( st0 );
fmul();

fldpi();
fld(4.0);
fmul();

fmul();

fstp( area );
stdout.putr32(area, 4, 10);
ret();
end computeSurfaceArea;

begin surfaceArea;

stdout.put("Lemme calculate the surface area of a sphere!", nl);
stdout.put("Gimme r: ");
stdin.get(radius);
stdout.put("Surface area = ");
call computeSurfaceArea;

end surfaceArea;
ceemorales
  • 11
  • 2
  • 1
    I don't know hla (and I'll never get the point of mangling x86 assembly on purpose) but: 1) Popping the return address and pushing it later just to get a parameter is very ugly. 2) unless `fmul()` resolves to `fmulp`, it seems that you are computing `4*pi*pi` and leaving stuff on the x87 stack 3) The area of a circle is `pi * r ^ 2`. 4) It seems that you are calling `computeSurfaceArea` without its parameter. – Margaret Bloom Aug 01 '16 at 07:03
  • sorry, you are computing the surface area of a sphere, not the area of a circle. Didn't see that. – Margaret Bloom Aug 01 '16 at 07:05

1 Answers1

1

Look here: stdout.putr32(area, 4, 10);

Unfortunately you have not provided enough field width for the output text (space for the leading numbers nor the decimal) for the value to be printed correctly.

stdout.putr32( r:real32; width:uns32; decpts:uns32 ); The first parameter to these procedures is the floating point value you wish to print. The size of this parameter must match the procedures name. The second parameter specifies the field width for the output text. Like the width when working with an integer value, this width is the number of character positions the number will require when the procedure displays it. Remember to include a position for both the sign of the number and the decimal point. The third parameter specifies the number of print positions to place after the decimal point. As an example, stdout.putr32( pi, 10, 4 ); displays the value _ _ _ _ 3.1416 where the underscores are used to represent leading spaces.

I hope this helps!

ReignTech
  • 21
  • 1