jemv7181
Posts: 5
Joined: Sun May 31, 2020 9:30 pm

FLOATING POINT

Sun May 31, 2020 9:34 pm

I want to make a floating point calculator using printf and scanf function with the Assembly language using QEMU emulation
Last edited by jemv7181 on Tue Jun 02, 2020 4:03 am, edited 1 time in total.

deepo
Posts: 572
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: FLOATING POINT

Mon Jun 01, 2020 11:30 am

I don't know anything about QEMU, but if you're looking for help on printf and scanf have a look here:
https://www.programiz.com/c-programming/c-input-output

To make a simple calculator you could implement a "parse a line" calculator, i.e. let the user input e.g. 4.5 * 5.333
Then your program should parse the two floats and the operator, then do the calculation and output the result.

/Mogens

jahboater
Posts: 5645
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: FLOATING POINT

Mon Jun 01, 2020 12:21 pm

The system manual pages on the Pi describe printf and scanf.

man 3 printf

man 3 scanf

Just use the type "double" for 64-bit floating-point arithmetic.
Pi4 8GB running PIOS64

jemv7181
Posts: 5
Joined: Sun May 31, 2020 9:30 pm

Re: FLOATING POINT

Tue Jun 02, 2020 2:51 am

Thanks for answering, I already knew how to do them, and they are instruction VFP for single precision and double precision
Last edited by jemv7181 on Tue Jun 02, 2020 3:40 am, edited 1 time in total.

W. H. Heydt
Posts: 12360
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: FLOATING POINT

Tue Jun 02, 2020 3:04 am

jemv7181 wrote:
Tue Jun 02, 2020 2:51 am
Thanks for answering, I already knew how to do them, and they are instruction VFP, VCVT, VMOV, ETC :D
That isn't C. Those are assembly language mnemonics.

Your project is just crying out to be done in SNOBOL.

Heater
Posts: 15809
Joined: Tue Jul 17, 2012 3:02 pm

Re: FLOATING POINT

Tue Jun 02, 2020 3:27 am

jemv7181 wrote:
Sun May 31, 2020 9:34 pm
I want to make a floating point calculator using printf and scanf function in QEMU.
HELP please
Your question is not making much sense to me. Perhaps you could clarify what you want to do a bit more?

The fact that you mention printf and scanf indicates you want to write whatever program in the C language.

It seems you want to write a calculator program. OK, do you mean just something simple that accepts expressions input from the command line and prints out results?

QEMU is a CPU and machine emulator, for example enabling the running of operating systems and programs built for an ARM CPU to be run on a PC with it's Intel x86 CPU. What has that got to do with you writing your calculator program?

What actually do you need help with?

If you want to know how to use printf and scanf functions then a quick google search will find you a lot of documentations, tutorials and examples about that. As noted above typing "man printf" into google will instantly get you to the manual page for it.
Memory in C++ is a leaky abstraction .

jemv7181
Posts: 5
Joined: Sun May 31, 2020 9:30 pm

Re: FLOATING POINT

Tue Jun 02, 2020 3:29 am

if you are right don't specify the language about the question i asked, and if it is in assembler the question i asked earlier, sorry for not specifying :(

jemv7181
Posts: 5
Joined: Sun May 31, 2020 9:30 pm

Re: FLOATING POINT

Tue Jun 02, 2020 3:34 am

Heater, You are very right, of course, do not specify and make your recommendation to look for tutorials and others, but as I said before, do not specify the assembly language, sorry for not specifying

Heater
Posts: 15809
Joined: Tue Jul 17, 2012 3:02 pm

Re: FLOATING POINT

Tue Jun 02, 2020 3:43 am

jemv7181 wrote:
Tue Jun 02, 2020 3:34 am
Heater, You are very right, of course, do not specify and make your recommendation to look for tutorials and others, but as I said before, do not specify the assembly language, sorry for not specifying
I did not understand that at all.

Do you mean to say that you want to write some kind of calculator in assembly language?

Can I assume you mean to use one assembly language, for the ARM CPU say, and run it under emulation using QEMU on a different machine, an Intel PC say?

Is this a home work assignment from college studies or some such?
Memory in C++ is a leaky abstraction .

jemv7181
Posts: 5
Joined: Sun May 31, 2020 9:30 pm

Re: FLOATING POINT

Tue Jun 02, 2020 3:50 am

Heater, yes, i am doing a calculator executing it in a Qemu emulation, and yes with Assembly language, however I was asking how to use instructions using the printf and scanf function, I already solved it but my question at the beginning was not written correctly.

Heater
Posts: 15809
Joined: Tue Jul 17, 2012 3:02 pm

Re: FLOATING POINT

Tue Jun 02, 2020 4:13 am

jemv7181 wrote:
Tue Jun 02, 2020 3:50 am
Heater, yes, i am doing a calculator executing it in a Qemu emulation, and yes with Assembly language, however I was asking how to use instructions using the printf and scanf function, I already solved it but my question at the beginning was not written correctly.
I would love it if you would post your solution here. I'd really like to understand what you have been asking about.
Memory in C++ is a leaky abstraction .

W. H. Heydt
Posts: 12360
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: FLOATING POINT

Tue Jun 02, 2020 4:41 am

jemv7181 wrote:
Tue Jun 02, 2020 3:50 am
Heater, yes, i am doing a calculator executing it in a Qemu emulation, and yes with Assembly language, however I was asking how to use instructions using the printf and scanf function, I already solved it but my question at the beginning was not written correctly.
Printf and scanf are C functions. You *should* be able to call a C program from an assembly language program (though such things are--traditionally--done the other way around). Since it appears that you want to write your calculator program in an assembly language, where does QEMU come in? Why not write it in the native assembly language for whatever machine you plan to run it on?

Or, as has already been ask, is this a school project with those specific requirements? (In which case, the proper place to ask you questions is your instructor or TA.)

Heater
Posts: 15809
Joined: Tue Jul 17, 2012 3:02 pm

Re: FLOATING POINT

Tue Jun 02, 2020 4:58 am

I have seen students tackling homework assignments in assembler that used simulators, usually for MIPS. I presume that is because it it is perceived that a nice simple RISC instruction set is easier to teach in a short course than the mess that is x86. Besides it's the concepts of machine operation not the actual details of a particular instruction set that are important here. And it does give some standardized environment on which to base course materials. Perhaps today they try to do the same with ARM under QEMU.

I have never seen such courses get into the nitty-gritty of calling C functions from assembler and vice versa. All that ABI stuff is rather advanced and usually out of scope.

Things like printf being extra complex as they are varadic.

I still have no idea what the actual question here is.
Memory in C++ is a leaky abstraction .

User avatar
rpdom
Posts: 16984
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: FLOATING POINT

Tue Jun 02, 2020 5:02 am

Is there even a Raspberry Pi or Raspberry Pi OS involved here?
Unreadable squiggle

Heater
Posts: 15809
Joined: Tue Jul 17, 2012 3:02 pm

Re: FLOATING POINT

Tue Jun 02, 2020 5:05 am

Who knows. OP does not say much.

Could be a Pi operating system running under QEMU on a PC.

Could be running QEMU on a Pi.

Could be a Pi user who only knows this community as a source of such programming advice.

I'm curious looking forward to getting to the bottom of this.
Memory in C++ is a leaky abstraction .

W. H. Heydt
Posts: 12360
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: FLOATING POINT

Tue Jun 02, 2020 5:20 am

rpdom wrote:
Tue Jun 02, 2020 5:02 am
Is there even a Raspberry Pi or Raspberry Pi OS involved here?
No indication one way or the other. So, yes, point. This may be so off topic it doesn't belong on the Forums at all.

W. H. Heydt
Posts: 12360
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: FLOATING POINT

Tue Jun 02, 2020 5:22 am

Heater wrote:
Tue Jun 02, 2020 5:05 am
I'm curious looking forward to getting to the bottom of this.
I get the distinct impression of a student who is in over his depth. I can think of several ways he could have gotten there...

Heater
Posts: 15809
Joined: Tue Jul 17, 2012 3:02 pm

Re: FLOATING POINT

Tue Jun 02, 2020 8:06 am

I guess the English translation of the Spanish post viewtopic.php?f=76&t=275906 sheds some light on this:
Is there any Assembly language ARM instruction that can potentially make the number n?
For example:
2 ^ 2 = 2x2 = 4
5 ^ 3 = 5x5x5 = 125

or only with the arithmetic operation "MUL", using the QEMU emulation
Although that is about powers rather than floats so perhaps not the same question.

It does show signs of being a progression in homework assignments.
Memory in C++ is a leaky abstraction .

User avatar
rpdom
Posts: 16984
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: FLOATING POINT

Tue Jun 02, 2020 8:33 am

Heater wrote:
Tue Jun 02, 2020 8:06 am
It does show signs of being a progression in homework assignments.
It does, and if it was me needing an answer I would do a web serach for ARM instruction manual (or something like that) and then find the ARM ARM for the version of ARM that the QEMU I was told to use is supporting and read up on which instruction it supports. Very important to get the right ARM version. The ARM1 didn't even have a MUL instruction. Then there's all the VFD and NEON options...
Unreadable squiggle

jahboater
Posts: 5645
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: FLOATING POINT

Tue Jun 02, 2020 10:07 am

Heater wrote:
Tue Jun 02, 2020 8:06 am
Is there any Assembly language ARM instruction that can potentially make the number n?
For example:
2 ^ 2 = 2x2 = 4
5 ^ 3 = 5x5x5 = 125

or only with the arithmetic operation "MUL", using the QEMU emulation
Although that is about powers rather than floats so perhaps not the same question.
Is the OP asking for an ARM instruction for integer power?
The answer is there isnt one, but its easy to do with multiply.
The algorithm in C is:-

Code: Select all

#define mul(a,b)  __builtin_mul_overflow( a, b, &a )
int base, exp;   
        if( exp < 0 )
          fail("domain error");
        int result = 1;
        while( base > 0 )
          if( ((base & 1) && mul( result, exp )) ||
              ((base >>= 1) && mul( exp, exp ))
            )
            fail("overflow");
      	printf( "result is %d\n", result );
The overflow check is important for obvious reasons.
Pi4 8GB running PIOS64

Heater
Posts: 15809
Joined: Tue Jul 17, 2012 3:02 pm

Re: FLOATING POINT

Tue Jun 02, 2020 10:41 am

jahboater wrote:
Tue Jun 02, 2020 10:07 am
The overflow check is important for obvious reasons.
Obvious?

Hardly anyone ever checks for overflow on regular addition, subtraction, multiplication or division. Why bother with powers?
Memory in C++ is a leaky abstraction .

jahboater
Posts: 5645
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: FLOATING POINT

Tue Jun 02, 2020 11:30 am

Heater wrote:
Tue Jun 02, 2020 10:41 am
jahboater wrote:
Tue Jun 02, 2020 10:07 am
The overflow check is important for obvious reasons.
Obvious?

Hardly anyone ever checks for overflow on regular addition, subtraction, multiplication or division. Why bother with powers?
Because integer power overflows with unexpectedly small values, for example: 11^9 is enough for 32-bit integers and 23^14 is enough for 64-bit integers.
I would use 64-bit integers for this, but the range is still pretty small.

For the other math operators, people should check, or use a language that checks, or otherwise prove it cannot happen.
Even abs(INT_MIN) overflows by the way.
Pi4 8GB running PIOS64

Heater
Posts: 15809
Joined: Tue Jul 17, 2012 3:02 pm

Re: FLOATING POINT

Tue Jun 02, 2020 11:54 am

That is odd, in Python I can get to 23^14 before things break:
>>> (2**64 - 1) - (23**14)
6853907749170801806
>>> (2**64 - 1) - (23**15)
-248188491390681693992
>>>
Likely people should check all kind of things they don't. Nobody check maths ops unless they really have to or after a bug shows up. Power is no different.
Memory in C++ is a leaky abstraction .

jahboater
Posts: 5645
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: FLOATING POINT

Tue Jun 02, 2020 12:18 pm

Heater wrote:
Tue Jun 02, 2020 11:54 am
That is odd, in Python I can get to 23^14 before things break:
Python3 uses bigints all the time so even 23 ** 1000 doesn't overflow.

I may have been off topic as the thread title is FLOATING POINT, yet all the OP's examples were small integers.
Pi4 8GB running PIOS64

jahboater
Posts: 5645
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: FLOATING POINT

Tue Jun 02, 2020 12:25 pm

Heater wrote:
Tue Jun 02, 2020 11:54 am
Likely people should check all kind of things they don't. Nobody check maths ops unless they really have to or after a bug shows up. Power is no different.
Checking is costly without help from the compiler:-

Code: Select all

static pure bool
check_mul_overflow( const int left, const int right )
{
  if( left == 0 || right == 0 )
    return false;
  if( left > 0 )
    return right > 0 ? left > imax / right : right < imin / left;
  return right > 0 ? left < imin / right : right < imax / left;
}
The compiler on the other hand will simply use a jump-on-overflow instruction.
Pi4 8GB running PIOS64

Return to “General discussion”