We mostly did this last stream, but we only got partway through converting a number into a string. You would think it would be easy, but I remember it being more involved. We got stuck on some things since we were not sure how they worked, but then we figured it out and had a few aha moments.
Right here on the screen, I realized I finally understood some of the operators, like subtract, where x3 equals x3 minus 1. That just decrements itself by one, which is a common thing in programming. Thanks for the validation, everyone.
Jatin, good to see you. And yes, we are using Pydantic and some type variables here, though these abstractions sometimes get me mixed up. Now we are looking at assembly code for turning a number into a string.
It takes a lot of steps. This single file is about 50 lines, just for the conversion. I think it should be more simple, but that is what the assembly code is doing.
In higher-level languages you could just use a two string method, but here you have to break down each part. For example, in assembly, you can assign a register a value like ASCII zero, which I did not know was possible. So to test, we built the program, ran it, and hit a segmentation fault right where the conversion happens.
Shimon pointed out you can just store numbers in decimal, like how calculators do, using binary-coded decimal, but you would have to adjust the math for that. For most computers, just converting to a string is fine. So after fixing things, and running the build, it works now.
We are printing a number, then converting it to string, then printing that. The function takes x0 as the value to convert, then uses assembly instructions to chop off a digit by dividing by 10 until there are no digits left. That is the main idea: keep dividing by 10 and get each digit.
On ARM64, we found out there is no modulo instruction, which is surprising since most CPUs have it. I use modulo a lot in other projects, so that was unexpected. Anyway, it all works: we print the string version of the number, print hello world, loop, and use BEQ to conditionally break out.
Pretty cool!
Информация по комментариям в разработке