<- previous index next ->
Assembly language can run with no C compiler and no OS
Special hardware may need assembly language
A first program that does not need a C compiler, that uses the
Operating System calls.
hellos_64.asm
; ------------------------------------------------------------------------
; hellos_64.asm
; Writes "Hello, World" to the console using only system calls.
; Runs on 64-bit Linux only.
; To assemble and run: using single Linux command
;
; nasm -f elf64 hellos_64.asm && ld hellos_64.o && ./a.out
;
; -------------------------------------------------------------------------
global _start ; standard ld main program
section .data ; data section
msg: db "Hello World",10 ; the string to print, newline 10
len: equ $-msg ; "$" means "here"
; len is a value, not an address
section .text
_start:
; write(1, msg, 13) equivalent system command
mov rax, 1 ; system call 1 is write
mov rdi, 1 ; file handle 1 is stdout
mov rsi, msg ; address of string to output
mov rdx, len ; number of bytes computed 13
syscall ; invoke operating system to do the write
; exit(0) equivalent system command
mov rax, 60 ; system call 60 is exit
xor rdi, rdi ; exit code 0
syscall ; invoke operating system to exit
A few basic BIOS calls:
See BIOS info
On UMBC computers, you do not have "root" privilege, thus
you can not use BIOS calls. On your own Linux systems,
you could do sudo ./a.out to run a program.
If you can cause a program to boot from a floppy, a CD, a DVD, or
a flash drive, you can assemble a program that will run without
an operating system. The boot process uses the BIOS and the BIOS
has functions that can be called via interrupts.
A sample bootable program is boot1.asm
; boot1.asm stand alone program for floppy boot sector
; Compiled using nasm -f bin boot1.asm
; Written to floppy with dd if=boot1 of=/dev/fd0
; Boot record is loaded at 0000:7C00,
ORG 7C00h
; load message address into SI register:
LEA SI,[msg]
; screen function:
MOV AH,0Eh
print: MOV AL,[SI]
CMP AL,0
JZ done ; zero byte at end of string
INT 10h ; write character to screen.
INC SI
JMP print
; wait for 'any key':
done: MOV AH,0
INT 16h ; waits for key press
; AL is ASCII code or zero
; AH is keyboard code
; store magic value at 0040h:0072h to reboot:
; 0000h - cold boot.
; 1234h - warm boot.
MOV AX,0040h
MOV DS,AX
MOV word[0072h],0000h ; cold boot.
JMP 0FFFFh:0000h ; reboot!
msg DB 'Welcome, I have control of the computer.',13,10
DB 'Press any key to reboot.',13,10
DB '(after removing the floppy)',13,10,0
; end boot1
Another bootable program is boot1a.asm
Very small bios test bios1.asm
This is for 64 bit computer:
; bios1.asm use BIOS interrupt for printing
; Compiled and run using one Linux command line
; nasm -f elf64 bios1.asm && ld bios1.o && ./a.out
global _start ; standard ld main program
section .text
_start:
print1: mov rax,[ahal]
int 10h ; write character to screen.
mov rax,[ret]
int 10h ; write new line '\n'
mov rax,0
ret
ahal: dq 0x0E28 ; output to screen ah has 0E
ret: dq 0x0E0A ; '\n'
; end bios1.asm
Another small bios test bios1_32.asm
This is for 32 bit computer:
; bios1_32.asm use BIOS interrupt for printing
; Compiled and run using one Linux command line
; nasm -f elf32 bios1_32.asm && ld bios1_32.o && ./a.out
global _start ; standard ld main program
section .text
_start:
print1: mov rax,[ahal]
int 10h ; write character to screen.
mov rax,[ret]
int 10h ; write new line '\n'
mov rax,0
ret
ahal: dd 0x0E28 ; output to screen ah has 0E
ret: dd 0x0E0A ; '\n'
; end bios1.asm
A more complete bootable program with subroutines and uses a
printer on lpt 0 is:
bootreg.asm
Project 3 is assigned
See Project 3
<- previous index next ->