EEVblog Electronics Community Forum

Products => Computers => Programming => Topic started by: westfw on March 05, 2021, 12:56:00 am

Title: Range checking...
Post by: westfw on March 05, 2021, 12:56:00 am
Has there ever been an Instruction set Architecture that includes an explicit "range check" instruction?
I'm thinking something that would cause an exception, rather than storing a result.
Code: [Select]
    rangecheck op1, op2, op3   ; raise an exception if op1 is not between op2 and op3.
    rangecheck op1, op2        ; raise an exception if op1 is not between zero and op2.
Ideally, op2, op3 could be immediate values, or whatever the CPU normally uses for operands (registers on RISC, anything on CISC?)
Title: Re: Range checking...
Post by: retiredfeline on March 05, 2021, 01:09:06 am
Here's one hit for a quick search: https://en.wikipedia.org/wiki/Bounds_checking
Title: Re: Range checking...
Post by: SiliconWizard on March 05, 2021, 01:13:47 am
In particular, you can take a look at Intel MPX: https://en.wikipedia.org/wiki/Intel_MPX
Title: Re: Range checking...
Post by: MK14 on March 05, 2021, 02:34:09 am
Yes, the X86 has such an instruction, available for a long time. Which creates an exception, if the bounds are exceeded.

https://www.felixcloutier.com/x86/bound (https://www.felixcloutier.com/x86/bound)

Here it is, for the 386 cpu (Intel):
https://pdos.csail.mit.edu/6.828/2018/readings/i386/BOUND.htm (https://pdos.csail.mit.edu/6.828/2018/readings/i386/BOUND.htm)
Title: Re: Range checking...
Post by: SiliconWizard on March 06, 2021, 06:00:08 pm
Yes, the X86 has such an instruction, available for a long time. Which creates an exception, if the bounds are exceeded.

https://www.felixcloutier.com/x86/bound (https://www.felixcloutier.com/x86/bound)

Here it is, for the 386 cpu (Intel):
https://pdos.csail.mit.edu/6.828/2018/readings/i386/BOUND.htm (https://pdos.csail.mit.edu/6.828/2018/readings/i386/BOUND.htm)

Note that the 'bound' instruction is not available in 64-bit mode AFAIK, so in this case, I guess the Intel MPX extension is the alternative for Intel CPUs. (It's more capable than just the original bound instruction too...) It was also known to be actually slower than using several instructions for bound checking starting with the i486...

I'm not aware of similar instructions in ARM and MIPS ISAs, but I'm not a specialist of those either, so if anyone knows?
Title: Re: Range checking...
Post by: DiTBho on March 06, 2021, 08:19:52 pm
Has there ever been an Instruction set Architecture that includes an explicit "range check" instruction?

Programmers can use the CHK2 instruction in the Motorola 68000 to check if a number is within a certain range.

Why do you ask it?