MPLab Directives for Multibyte Integers [SOLVED] - Page 1
Forum section: electronics / general

author: jpanhalt
author_rank: Super Contributor
timestamp: January 31, 2024, 11:26:36 am
content: When writing Microchip PIC Assembly, I often use the "high" and "low" preprocessor directives to facilitate entering integers, for example in decimal radix:Code:movlw high(1234)MPLab 8.92 accepts: low, high, and upper for three-byte integers. I suspect there are no such operators for 4-bytes and more. However, is there a convention for naming the bytes of 4-byte numbers, for example something like low, high, upper, and ****?Regards, John

author: Ian.M
author_rank: Super Contributor
timestamp: January 31, 2024, 01:47:12 pm
content: I don't remember seeing one. MPASM is only for 8 bit PICs, and it seems that at least in the application notes, 32 bit integers are rare except as the result of a 16bx16b multiply, and generally handled as a pair of 16 bit values with explicitly declared addresses for each byte.If you need that functionality, try:Code:#define top(NN) ((NN)>>.24)Then you can do stuff like:Code:movlw top(0xDEADBEEF)Unfortunately you cant emulate the prefix operator style ofupper,highandlow, but as they don't mind you bracketing their expression, you can use function like syntax with them to match your #defined top().N.B. The MPASM expression evaluator only uses 32 bit unsigned maths internally so you need to be very careful about order of operations to avoid overflow when using MPASM to evaluate the top byte of 32 bit expressions during the build.

author: jpanhalt
author_rank: Super Contributor
timestamp: January 31, 2024, 02:21:32 pm
content: Thanks, Ian.I suspected that was the answer. Fortunately, my current project only requires 24-bit. However, I have wondered about whether the top byte in a 32-bit integer had a common name. I usually just number the bytes 0...3 when using more than 3. That comes into play when doing fixed point division and similar manipulations.It's related to my longer term project for reading a Mitutoyo linear scale encoder and using those readings to control my lathe. The values for that will be considerably less than 10" in practice, but I am writing code to handle a range of 0 mm to <999.99 mm and the inch equivalents. Specifically today, I am working on setting up "presets" for machining and needed to convert a preset entered in inches (e.g., 30.1235" to 765.1369mm rounded to 765.14mm). Of course, anything less than 0.01 mm will be rounded.

author: DavidAlfa
author_rank: Super Contributor
timestamp: January 31, 2024, 03:58:43 pm
content: Quote from: jpanhalt on January 31, 2024, 11:26:36 amJust Google "mplab asm upper high low" :https://ww1.microchip.com/downloads/en/DeviceDoc/33014L.pdf6.4 LOW, HIGH AND UPPER OPERATORS.So I guess there isn't.

author: jpanhalt
author_rank: Super Contributor
timestamp: January 31, 2024, 04:32:15 pm
content: Hi David,That's the exact version of the MPLab manual I use. There is a little more history to it. Back in 2016, I was playing with the AMS AS3935 lightning detector (aka "Franklin" detector) and noticed in the register map the apparently strange names for 3 registers: L, M, MM in that order (attachment). I sent a message to AMS, and the reply was: least (LSB), middle (MSB), and most (MMSB). I was thinking MM might be "most most" considering the authors probably were fluent in German.That prompted me to read that section of the MPLab manual to get the correct Microchip names.John