Coding in HEX

Hy! Can somebody give me the most simple code in HEX for RPi, model B, ver.2?I want to write it in sector 0 of an empty SD Card, and trying to see if something hapen.The code can be everything: "Switch ON the OK Led", "Print a character on screen" etc.If is posible to look like this (as little endian 32 bit words):00000000: 00010203 04050607 08090a0b 0c0d0e0f ...

Note that the boot code (i'm assuming that this is what you are trying to test) ISN'T written in ARM code, but instead is code written for the Graphics processing core, and almost nobody knows how that code works!So writing data to sector 0 of an empty SD Card, and trying to see if something happen is pointless, as ARM hex code there won't work! Also the PI has executed at boot time not a byte of code to initialize stuff like the video system, because all this must also be done using the gpu, not the ARM cpu.

Actually, sector 0 contains an MS-DOS style MBR and partition table. It can be used to boot quite a few OSen on a PC, but the boot block isn't used at all on the Pi, apart from possible to identify the fact that it is an MS-DOS type MBR and has a valid partition table.

The only bit that the Pi needs in it is that partition table to show that the first partition is FAT and where it is and how big.

No executable code is used from sector 0.

For creating "simple" code to run on a Pi, look at the Assembler tuts at http://www.cl.cam.ac.uk/freshers/raspbe ... orials/os/ Programming in hex is possible, but would only be useful as an exercise as you would essentially be doing by hand what the assembler does for you.

I used to write Z80 code in hex on ZX80/ZX81/ZX Spectrum and Sharp MZ-80K. Ouch, it was tedious and prone to error. Having to recalculate the all relative jump instructions every time I added or removed a byte here or there...

Actually, I read already the tutorial for Baking Pi – Operating Systems Development, but I didn't understand nothing. I instal that Toolchain, but I never understand how to use it. So I decide to program my Raspberry Pi only in HEX. I want to write an OS impossible to brake, so only in HEX I can see exactly what I write.Before I write small OS in Boot sector, on HDD, on x86 using only 8086 instruction set, using Debug program from Windows, and it was very easy for me, but I see on ARM is too hard too understand.

The only thing I find for boot is this: "After the Raspberry Pi boot sequence loads the kernel.imgand jumps to the address 0x8000, the Raspberry Pi bootloader has served its purpose and the control is given to theLinux kernel."

HEX_addict wrote:The only thing I find for boot is this: "After the Raspberry Pi boot sequence loads the kernel.imgand jumps to the address 0x8000, the Raspberry Pi bootloader has served its purpose and the control is given to theLinux kernel."

This is exactly what you need to know.

Encode your "HEX" stuff to run at 0x8000, save it as kernel.img (preferable in ELF format - look it up). Boot.

Seriously, you should look at the Baking Pi tutes, they aren't that difficult and you will pick up ARM code quite quickly if you forget x86 and the way that works.

ARM code is one of the leanest and neatest instruction sets I've used (and that is quite a lot). Learn the instruction set and you will get used to it. I've been using it on and off since the very first ARM chip was released

rpdom wrote:Encode your "HEX" stuff to run at 0x8000, save it as kernel.img (preferable in ELF format - look it up). Boot.

Seriously, you should look at the Baking Pi tutes, they aren't that difficult and you will pick up ARM code quite quickly if you forget x86 and the way that works.

ARM code is one of the leanest and neatest instruction sets I've used (and that is quite a lot). Learn the instruction set and you will get used to it. I've been using it on and off since the very first ARM chip was released

ELF format = Litle Endian Format ??

Can u write for me that "HEX stuff"? Plsss. Make a small HEX program to show on screen the text "Hy!", or evrything small and easy to understand. I will write your code with Hxd Hexeditor in Sector 0 to see if something happen.

As you were told , the "boot sector" of the SD card iscompletely ignored. Only the contents of the file kernel.img count. I think you just need to type the code in the video inyour HEX editor and save it as kernel.img.

Most people here use ARM Assembly , and there is virtually zerodocumentation on "coding in HEX". So at least a basic understanding of ARM assembly is important to translate any advanced examples in your head to binary/"HEX code".

• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

I understand. So, I must to quit programming. I will sell my Raspberry Pi and I wil continue programming with Debug from Windows on x86 processors. In case u find some good information about WRITING IN HEX ON RASPBERRY PI, please post it here.

Thats NOT what i said. I just meant learning ARM assembly will make your life easier. The video you linked to shows how "code up" thingswithout it , but it is the first tutorial of its kind that i have seen. Most seem to deal with writing assembly instead.

People wrote computer programs before assemblers were invented and the video shows that you still can do this on a Pi.I wish you good luck , but be aware that this is mostly "uncharted territory" - you being one of the first cartographs.

ghans

Last edited by ghans on Sat Jun 21, 2014 2:41 pm, edited 1 time in total.

• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

rpdom wrote:Actually, sector 0 contains an MS-DOS style MBR and partition table. It can be used to boot quite a few OSen on a PC, but the boot block isn't used at all on the Pi, apart from possible to identify the fact that it is an MS-DOS type MBR and has a valid partition table.

The only bit that the Pi needs in it is that partition table to show that the first partition is FAT and where it is and how big.

No executable code is used from sector 0.

you are quite right rpdom, I did understood what HEX_adddict was trying to do, but obviously his thinking wasn't actually based on factual information. I stand corrected.

I remember a colleague that could read a hex-dump of ZX80 code, as if it was assembler. Me, I never remembered more than that "C9" was the return instruction and "CD xx yy" was the call instruction (if memory serves me well). Also I used lined notebook paper to hand assemble 6502 code for my KIM-1, which I then punched into its hexadecimal keypad. that was fun! but very laborious.

So, I'm alone. Do you know maybe, after GPU load the boot sector into adress 0x8000, who will execute the code from memory adress 0x8000, the GPU or CPU? Because if is execute from the GPU, I can not write code in ARM instruction set, right?

After the GPU is up , the ARM takes over. You can write ARM code into kernel.img and it will be excuted from 0x8000.Note that kernel.img is just a file on a FAT filesystem. No need to use low-level disk writing.

ghans

• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

After the GPU has booted and initialized the video system it transfers control to the ARM, which starts executing code from the 0x8000 address, where the GPU has placed the code read from the SD-card from the file "kernel.img". Normally this code contains the linux kernel (that's why it is called "kernel.img"), but it can contain any kind of ARM code!

This is specifically "bare metal" terrain, that is the reason why we have a bare metal forum, where this thread IMHO belongs!

also note that all the "BIOS interrupts" you might be familiar with to do things like basic terminal I/O are not present in a PI, which has no BIOS! Normally this would be catered for by the Linux kernel, but since that isn't loaded, yes you are "alone" in that sense! That's why you need bare metal knowledge for even the simplest tasks, perhaps excluding blinking the ACT LED, which is connected to a GPIO-16 (active low).

mahjongg wrote:I remember a colleague that could read a hex-dump of ZX80 code, as if it was assembler. Me, I never remembered more than that "C9" was the return instruction and "CD xx yy" was the call instruction (if memory serves me well).

Ah, those were the days. Yes, CD was the call instruction and C3 was the jump instruction. 00 was noop. I can't remember any of the others these days, but it has been a long time (goes all misty eyed now).

• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

No, didn't work. He have 2 mistake in his tutorial, but I correct them. He said something, but write diferent. Also, he use another functions , not the original asm code from oficial Baking Pi - Lesson 1. The code from his youtube tutorial I write it above.By the way, I format the SD card, I put (with copy-paste) only the file ".bin" from RISC OS card, witch I use (because have BBC BASIC with a easy ARM Assembler), and... surprise: when i boot from the ".bin SD card", nothing show me on screen, but Green LED OK/ACK is blinking. )))))I saw on the internet that with Bare Metal u can make your own Operating System, but when I saw that tutorials are in C++, I quit. I know a litle VB 6.0 and I work before in x86 ASM, with Debug.

The BBC BASIC built-in ARM assembler was how I started in ARM code back in the 1980s, but as I mostly use Linux now I use (g)as which I'm finding usable.

If you want things to appear on the screen you will have to write your own screen driving routines. As someone mentioned earlier there is no BIOS to handle the basic hardware functions like you do on old x86 systems, and yes, I've used both debug and BBC BASIC (x86) assembler to write various x86 code and small "embedded" stuff.

To print a character on the screen you'll need to use some of the "mailbox" routines (they are one way the ARM CPU can exchange information with the GPU) to get/set a framebuffer size/depth/start-address. Then you store bytes in memory at offsets from that start-address to plot pixels on the screen.

I would paste some example code, but I'm away from home and haven't got any with me.[edit]I've never been able to fully understand C code, but I'm absolutely fine with assembler - I can see what it's doing rather than using arcane symbols and complex terminology

That's why I hate also C, and because of C exist hackers. If I have somebody to help me I can build a server imposible to brake.I will do it anyway starting from next year when I will have enough money to eat, and stay relax.I need only one month to read books of Assembler for x86, one month to makesmall exercises in ASM, and one month to programming the server.Sometimes I'm asking...how much cost me a machine who can build me my own processor/SOC and my own Raspberry Pi? I can make it 10-20 times faster only by chanching the logic from inside him. I lose time from my brain trying to find how the processor working, instead make programs on Raspberry Pi. If a company will sell a kit to make your own Raspberyy Pi, i will change the world. Windows and Linux will be just history. I cane make every kind of program, but only in HEX. If u find time, put here the code in HEX and ASM, to print a character on screen.

Actually a number of us have been able to program the GPU using its instruction set thanks to HH and his work and the folks helping out there, now I believe Broadcom has published that info.

Writing in "HEX" wont in any way shape or form make your operating system safe or protected or anything like that. That isnt possible on this or most hardware on the planet, and if it were then it is only possible for a short period of time, these days that is anywhere from hours to weeks (once there is interest). The time to defeat protection is generally a fraction of the time to create it. This form of hacking happens at the machine code level (generally disassembled).

The kernel.img file is not an .elf file it is just a raw binary so if for example you take the instruction for branch to self 0xEAFFFFFE and you place it in a file with the proper endianness it will actually run, now it wont do anything very exciting but it wont crash either.

it is harder, esp if you are learning an instruction set to learn machine code first, then assembly later than to learn assembly first then machine code later. Knowing C makes the transition to a new instruction set even easier because you can compile and disassemble and learn the assembly language from that as well...

It is quite trivial to generate machine code if that is what you mean by hex for the raspberry pi. The documentation is all out there for the arm instruction set the kernel.img file is just the raw memory image that is loaded into the ram. Just put the bytes in the file and power it on.

The arm instruction set is significantly easier than the x86 instruction set, much easier to program, if you can program x86 machine code you can do pretty much anything else.

If you cant operate an assembler or compiler you are not going to get very far anyway, spend a few more seconds and get those working. For example, simply load an sd card with the off the shelf raspbian or whatever os, which has the tools already built and ready to use. Or just get the pre-built ones from codesourcery (now mentor graphics) or https://launchpad.net/gcc-arm-embedded. Much easier than trying to encode branches and other instructions if you dont have a working assembler to show you how to do it. You only need binutils if you dont want to use C.

I was funy right? Butttt.... If I was speak right? You know, you have right. I will make a site where i will put my exercises in x86 HEX & ASM. I will sell All my books and things, to have money to eat, and I will keep only my bed, my TV, my cheap China chear , my out of order table, my pet fishes and my computers. From tomorrow I will wake up at 8:00 and I will sleep at 22:00-23:00. Three months I said?...3 months will be.God please, be with me!Please, don't close here till I will put the link from my site.