This page was last updated on March 13th, 2020(UTC) and it is currently May 30th, 2023(UTC).
That means this page is 3 years, 78 days, 4 hours, 16 minutes and 4 seconds old. Please keep that in mind.
26 - Hello in Assember
Now, to get to coding. First, we need a basic "hello world" program. We're not going to use the existing one, yet. I want to go even lower level. First, let's make a directory for containing this beast (and we're going to do some things that weren't yet covered). I recommend placing this directory in "C:\PROJECTS" and going into it. You'll want to create a file, I'm calling mine main.S, but you can call yours whatever you want as long as you keep track of all this. Inside I'm putting the following:
.section .text
.asciz "MEOW"
The assembler ("AS") we're going to run directly for now. Normally we rely on GCC to run it, but GCC can be cranky when we're not declaring "main." That's for later. In AS, commands specific to AS (not the processor's assembly) all begin with periods. In this case, since AS always outputs COFF files (which I'm guessing will change around 2038, so keep in mind that this could change a little by then), we must use the format to get what we really want. COFF allows for multiple "sections" (as do most formats, in the end), and this time we want to create one called ".text", which has traditionally been "executable code" goes (and I have no idea why they chose ".text"). "asciz" is a command for AS that says "Put the ascii values for these letters [every letter has a numeric value asigned to it] at this exact spot in the output followed by a single byte of 0."
First, you're going to run "as -o main.o main.S" which is invoked more or less like GCC in this case. Next, you are going to run "objcopy -j .text -O binary main.o main.bin". Since you're not going to be using this program alot, I'll let you look into that yourself if you want to know how exactly objcopy works and all that. For now, the goal is to get it to output the ".text" section as raw data. If you type in "xxd main.bin" (xxd comes with vim) you'll see output in the format of "line number: hex-decimal text". What's going on here is the hex represents the ascii values, and you'll see that you have 0x4d ("M"), 0x45 ("E"), 0x4f ("O"), 0x57 ("W"), the 0 character ("0" has a different hex-decimal value in ascii), followed by a series of 0x90. Now, for some reason, the DOS version of AS forced the file to be aligned to 16 bytes using 0xC0 for filler. This doesn't always happen everywhere these tools are deployed, but in this case it did. I tried looking up some ways to get rid of it, but sometimes some people get ornery like this. There are a few ways to solve the problem if we really needed to, but, odds are, it's not a pretty solution.
Get your own web kitty here!
©Copyright 2010-2023. All rights reserved.