Whenever you start programming, there is usually the first program that prints the phrase "Hello world" to the screen. Well, let us keep that tradition and write an entire assembly program that print that message to the screen.
The preceding is the standard file format of an assembly program using the Netwide assembler, or NASM.
To write something to the screen, we first need to store the value of what we want to render to the screen by declaring variables.
Next, we want to use some real world practical assembly coding to print this message to the screen. We could simple using the Linux int80h instruction to tell the operating system to print this message (if you aren't sure what I mean by this, do not worry), however we will use the printf function which is part of the C standard library. This method will teach us how to mix assembly and C.
So let us get started:
;Our Assembly Program file
SECTION .data
SECTION .bss
SECTION .text
The preceding is the standard file format of an assembly program using the Netwide assembler, or NASM.
To write something to the screen, we first need to store the value of what we want to render to the screen by declaring variables.
;Our Assembly Program file
SECTION .data
ourHelloMsg: db "Hello world, we are in assembly", 10, 0 ;our simple message
SECTION .bss
SECTION .text
Next, we want to use some real world practical assembly coding to print this message to the screen. We could simple using the Linux int80h instruction to tell the operating system to print this message (if you aren't sure what I mean by this, do not worry), however we will use the printf function which is part of the C standard library. This method will teach us how to mix assembly and C.
So let us get started:
;Our Assembly Program file
SECTION .data
ourHelloMsg: db "Hello world, we are in assembly", 10, 0 ;our simple message
SECTION .bss
SECTION .text
extern printf ;this tell our compiler that printf is available remotely
global main ;this tells our compiler to make "main" available to others
main:
;create the stack frame
push ebp
mov ebp, esp
;push the address of the msg onto the stack
;-->NOTE: label are aliases for memory address
push msg ;so here, msg stands in place of something like 0x3048503 call printf
;destroy the stack frame
mov esp, ebp pop ebp ret
Now to compile this program, pop open a terminal and bash out these commands:
nasm -f elf -o asm1.o asm1.asm
gcc -o asmProgram asm1.o
./asmProgram
Comments
Post a Comment