How to access pointers in SUBLEQ - pointers

I've recently started to learn about SUBLEQ One Instruction Set Computers and am currently trying to write a simple assembler for a SUBLEQ emulator i wrote. So far I've implemented DB, MOV, INC and DEC, but I am struggling a bit with the MOV instruction, when it has pointers as arguments.
For example: MOV 20, 21 to move data from address 21 to address 20 in SUBLEQ looks like this (assuming address 100 is zero and the program starts at address zero):
sble 20 20 3
sble 21 100 6
sble 100 20 9
The content at the target address is zeroed and the content at the source address is added to the destination by subtracting it two times.
Now to my problem: If one argument is a pointer, for example MOV 20, [21] so that the contents of address 21 are pointing to the real data I want to to copy to address 20, how can that be represented using SUBLEQ?

I'll start off by saying I know very little about how subleq is used in practice, so take this answer with a grain of salt:
One downside of subleq is that it is notoriously difficult to use pointers, but it can edit its own code. This means that you will have to use the code to rewrite the address being looked at with the value at 21.
for example, if you somehow got the code to go to a line appended after your current code you could use this:
# (I used the quotes to mean next line)
sble 3 3 " # set the first value of the second instruction to 0
sble 21 101 " # put the value of 21 into an unused address
sble 101 3 " # subtract the value of 101 and put it back into the code
sble 3 3 " # reset the value at 3 to 0
it might be a good decision to have to have a movp (move-pointer) command, so you don't accidentally mess up your mov command code at runtime
this means that a new method of using pointers has to be thought of differently for every problem someone comes across, but will usually be done by editing the code with the code

Related

rewrite line in SdFat for arduino

My problem is that I am trying to make Controller using Arduino, ESP8266 SDcard module and some sensors. When I try to store some data in SDcard, at first time all works fine, but in second or third time I need to rewrite same line with different values. But there is an issue because line length is not equal with previous.
If it is longer then nothing wrong, but if shorter, then it will leave some unnecessary characters.
The most difficult part is where I need to store value of LED light and time:
255 10 0 Where 255 represents LED-value, 10-Hour, 0-min
Value can be 1 or 3 character long, hour 1 or 2, min 1 or 2...
So is there any solutions for this problem??
Now I am trying to change int to uint8_t to equal all possible values.
Is this approach Right? Maybe someone has made something like that?
Any suggestions will be appreciated.
You can normalize the data as you suggest so that the line length is always the same.
One approach is that all values are uint8_t which would require 3 uint8_t values.
Another is leave it as a string but each field is a fixed width with padded values. e.g. 0050901 for a value 5 at the 9th hour, 1st minute. Or pad with spaces so 5 9 1 for the same representation of this data. (Two spaces before the 5 and one space before the 9 and the 1).
Either approach is fine and just depends on what you prefer or what is easier when consuming and/or writing the data.

Arduino - 5 questions for real Wire.write() and Wire.read() explanation

I Googled this a lot, and it seems that I am not the only one having problems with really understanding Wire.write() and Wire.read(). Being novice, I almost never use libraries that are already written by somebody, I try to create my class for module in order to truly understand how this module works and to learn how to manipulate with it. I've read few books and too many tutorials, but I could summarise these in two:
a) all tutorials are just showing the very basics of how to use these methods and b) they don't actually explain the steps, like everything is totally self explanatory. Call me stupid, but I have the feeling like somebody told me that 1 + 1 = 2, and then gave me some polynomial equation to solve :(
All book examples and almost all tutorials look like this imaginary example:
Wire.beginTransmission(Module_Address); //Use this to start transmission
Wire.write(0); // go to first register
Wire.endTransmission(); // end this
//To read
Wire.requestFrom(Module_Address, 3); //Read three registers
Wire.read(); //Read first register
Wire.read(); //Read second register
Wire.read(); //Read third register
And that's it about reading.
When it comes to writing, it's even worse:
Wire.beginTransmission(Module_Address); //Use this to start transmission
Wire.write(0); // go to first register
Wire.write(something); //Write to first register
Wire.write(something); //write to second register
Wire.endTransmission(); // end this
So far, working with ANY module I got, it was NEVER that easy. Usually, every register has more than one "option" inside. For example, lets say that imaginary module has First read register like this:
ADDRESS | BIT 7 | BIT 6 | BIT 5 | BIT 4 | BIT 3 | BIT 2 | BIT 1 | BIT 0
data Byte1 | mute .| option2.........|.....................option3.......................
To read only option 3, I would use this code:
Wire.beginTransmission(module_address);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(module_address, 1);
byte readings = Wire.read() & 0x1F; //0x1F is hexadecimal of binary 0001111 for option 3 in register 1
QUESTION 1
What does this '&' after Wire.read() REALLY means? (I know that it points to option within register, but I do not really understand this, why is it there)
QUESTION 2
Why the previous problem isn't written anywhere? So many tutorials, so many books, but I "discovered" it by accident when I tried to figure out how one library was working.
QUESTION 3
Imagine that hypothetical module has third register in write mode looking like this:
ADDRESS | BIT 7 | BIT 6 | BIT 5 | BIT 4 | BIT 3 | BIT 2 | BIT 1 | BIT 0
data Byte3 | write flag.......| option2.........|......................option3........
How to write flag without affecting option 2 and option 3? Or in other words, how to write to register 3's write flag? If I take 11000000 could affect because maybe I do not know what exactly option 2 and 3 do, or I do not wish to interfere with default setup.
QUESTION 4
Certain modules have to be written in binary-coded decimal. Let's say, that you have a timer and you wish to set 17 seconds for countdown to 0.And to do that, you need to write number 17 to register one, but number should be binary-coded decimal. 17 as binary-coded is: 0001 0111. But when you do this:
Wire.beginTransmission(module_address);
Wire.write(0);
Wire.write(00010111);
Wire.endTransmission();
You get different number, 13 or 10 (can't recall what number, I know it was wrong).
However, when doing this conversion: 17/10*16 + 17%10 it writes correct number 17.
Yes, I also accidentally found this out. BUT, where is this equation from? I searched (obviously wrong) as much as I could, but there was nothing about it. So, how did somebody come with this equation?
QUESTION 5
Probably a dumb off-topic question, BUT:
should Arduino library be written in a way that others could find it difficult to figure out the idea behind it? In other words, to figure out what the developer was exactly doing? I remember that one person used a lot of messy code to read something from sensor and then formula to convert it from binary-coded decimal to print it to Serial Monitor, while the same thing could be done with simply
Serial.print(read_byte, HEX);
It's not that I am smarter (or better) than them, I just don't understand why somebody would write a complex code when there is no(really) need for that.
Thanks a lot for any help :)
Questions 1. - 4.: Are all covered by Bit Manipulation tutorial on AVRFreaks forum Tutorials. So in short:
1) The & is used for bit masking in this case.
2) If you look for "Bit manipulation" then there are loads of Tutorials.
3) It's possible by Bit manipulation. How? For in memory variable just use bit masking. To clear two bits: var &= 0b00111111 to set two bits: var |= 0b11000000. If you don't have register value, you have to Read & Modify & Write it back. If you can't read the value (for example it's internal address like for eeproms) you have to have this value in memory anyways.
4) In C++ numbers starting by zero are in Octal base. If you want binary, you have to use 0b00010111. For the HEX base you have to use 0xFF. This is not explicitly mentioned in that tutorial, but both are used here.
5) It should be as clear as possible. But for the begginers without good knowlege of C++ it's hard anyway. For me is most difficult to read the code without indentation or even worst with bad indentation, bad variable names. The libraries are usually written by advanced users, so it's not so hard to understand with some background like knowing the datasheet for used MCUs and so on.
BTW: wrong comments are also bad for understanding:
//0x1F is hexadecimal of binary 0001111 for option 3 in register 1
The value 0x1F is definitely not 0001111 in binary but 00011111 (or better: 0b00011111)

Stop metro in max msp

I'm working on a project for my university. I'm connecting an Arduino to Max MSP using SimpleMessageSystem.
I read the values from the analog ports and it all works fine.
Although there is a metro 100 with the analog read to continue reading the values of it, as they change the whole time.
My problem is... that with the reading it also bangs every millisecond or so. But I would like that bang to stop after one of my analog reads, to play a sound when it reaches a specific number.
So what I want: I want ONE bang when the sensor reaches <340 if above 340 then 0.
I hope anyone can help and understand me.
I've managed it...
From the number value from my reading I split the outcome so I use:
split 0 340 and split 340 400
The first split with outcome message 1 and the second split message 0
You then use s num to select te number and r num to receive the number elsewhere.
You use change 666 and then select 0 1
And you connect your select to the toggle.
If it's an integer stream and you want one event triggered past 320 you could do:
[int] -> [> 340] -> [change] -> [sel 1]
The > 340 is because I don't know how consistent the integer stream is. Otherwise you could explicitly state the numbers to trigger once:
[int] -> [sel 340 ... ...]
This would create individual hard-coded patch cables for the events, refactoring that could be separate questions for the approach styles.

Order symbolic output for complex number Maple 18

my problem concerns operation with complex number in Maple 18. The issue is the following
i define this complex:
c:=a+i*b;
then i compute the square: sort(evalc(c^2))
and the output is:
a^2+2*i*ab-b^2;
So, how can i obtain an output like the following?
a^2-b^2b^2 +a*i*ab;
In other word i want an output where the real part precede the complex part.
i have tried with sort command but it was not enough ... probably exixts some command to format the output in complex manner but i dont find that ...
thank you in advance :)
In Maple 18 you could use the new InertForm package to get more control over the formatting.
The exact look will depend on the interface. In the commandline (TTY) interface both uses of Display below look the same, but both display with extra round brackets around the real part. In the Standard Java GUI the first one has a grey + to denote the inert %+.
restart:
c:=a+b*I:
expr:=c^2:
U := `%+`(evalc(Re(expr)),evalc(I*Im(expr))):
with(InertForm):
Display(U);
2 2
(a - b ) + 2 I a b
Display(U, inert=false);
2 2
(a - b ) + 2 I a b
Value(U);
2 2
-b + 2 I a b + a
This is a new way of handling such issues. In older Maple the real and complex parts could have been kept separate in the display by being each wrapped with the ``() operator. And then the actual value could be re-obtained by applying the expand command to strip that off. That's not so nice, displaying with extra brackets even in the GUI.

Converting virtual address to page table entry

I am reading Modern Operating Systems 3rd Edition by A.S Tanenbaum, and I've come to the chapter on virtual memory management. I've been stuck on a part for some time now, and I can't get my head around it. Either, it's a typo in the book, or I have misunderstood something.
Suppose we have a multi-level page table with two levels, where we map 32-bit virtual addresses to physical memory frames.
The fields for the page tables and the offset is
10 | 10 | 12
meaning we have a top level page table with 1024 entries, and a page size of 4096 bytes or 4KB. Entry 0 of the top level page table points to the text segment page table, entry 1 to the data segment, and the 1023 entry to the stack page table.
This is quoted from the book:
As an example, consider the 32-bit virtual address 0x00403004
(4,206,596 decimal), which is 12,292 bytes into the data. This virtual
address corresponds to PT 1 = 1, PT2 = 2, and Offset = 4. The MMU
first uses PT1 to index into the top-level page table and obtain entry
1, which corresponds to addresses 4M to 8M. It then uses PT2 to index
into the second-level page table just found and extract entry 3, which
corresponds to addresses 12288 to 16383 within its 4M chunk (i.e.,
absolute addresses 4,206,592 to 4,210,687).
When I translate the address to binary, I get 0000000001|0000000011|000000000100 which for me corresponds to PT1 = 1, PT2 = 3, Offset = 4.
Am I missing something, or is this a typo in the book stating PT2 = 2, when it actually should be PT2 = 3? As the text later says, the MMU uses the PT2 index to extract entry 3.
Where does the "12,292 bytes into the data" come from? How is that derived from the virtual address? I understand it has something to do with the offset, but I can't figure out how it's done. As far as I have understood, the physical address is derived as a combination of the frame number from the second page table, and the offset. I see that the 12,292 is a result of 3*4096+4 (PT2 entry * page size + offset). Is this correct?
1) I think this is a typo indeed, as 0000000001|0000000011|000000000100 binary = 4,206,596 decimal
2) The response is in the previous paragraph of the book :
Entry 0 of the top-level page table points to the page table for the program text, entry 1 points to the page table for the data, and entry 1023 points to the page table for the stack
So he is just saying that 0000000001|0000000011|000000000100 corresponds to 0000000011|000000000100 into the data. Indeed 0000000001 corresponds to the data in the top level page table, and 0000000011|000000000100 binary = 12,292 decimal.
I didn't finish converting to binary out of despair and a little bit laziness but its clearer now thanks to you. Seeing it made things much more clear.
it is a typo as the previous answerer stated. its without a doubt (Check US 4th edition)
"12,292 bytes into the data" comes from substracting 222 (4MB) (Toplevel Page table entry size=4MB) from 4206596
TLDR: 4206596 - 222 (4MB) = 12,292

Resources