Assembly Basics, hello world!

This is the level 0 intro!

There’s only two handfuls of assembly instructions you really need to know. Many others aren’t used as often. Most instructions are made up of three or four characters with an operand, a comma, then another operand.

To put some data into a register you use the MOV instruction.

mov ax,1	; put 1 into ax
mov bx,2 ; put 2 into bx
mov cx,3 ; put 3 into cx
mov dx,4 ; put 4 into dx

Push puts data at the top of a stack. Pop moves the data from the top of the stack into a specified register. To exchange two registers, ax and cx:

push cx ; put cx on top of the stack
push ax ; put ax on top of the stack
pop cx ; move data from the stack into cx
pop ax ; move data from the stack into ax

But this could be done easier with the XCHG instruction:

xchg ax,cx

To move data from one place to another, or copy, use MOV. To make the value of ax the same as what’s in bx:

mov ax, bx ; $ax=$bx in other languages

To call a BIOS function that does some pre determined stuff such as printing something, you use INT. Most functions have multiple methods you can call so you’ll need to figure out which routine you want and put that number into the ah register first:

mov ah,9 ; nine is the print subroutine
int 21h ; call the interrupt

The EQU instruction creates absolute symbols and aliases by assigning an expression or value to the declared variable name. Everytime you call your custom alias, it gets replaced with the expression on the right hand side:

H22 : EQU 22H

The obligatory Hello World example:

.stack
.data

MyString DB "Hello Kitty$" ; because i'm tired of Hello World!
CODE
.model small
.stack
.code

start:
mov dx,OFFSET MyString ; DX contains the offset of MyString
mov ax,SEG MyString ; AX contains the segment of MyString
mov ds,ax ; DS:DX points to the message
mov ah,9 ; nine is the print subroutine
int 21h ; call the interrupt
mov ax,4c00h ; this will terminate the program nicely
int 21h; call the interrupt

END start

The END keyword informs the assembler that there are no more source statements for that section. By itself, it should be the very last instruction. You don’t have to call it “start”, you could name it whatever you want as long as you don’t use a word that the assembler will think is some sort of instruction. You can have multiple sections and allow jump points based on register comparisons also. But I’ll leave that for level 1.

And what better to go with an intro to an ancient computer programming language than an ancient x86 assembler site with a full tutorial. It hasn’t been updated in years, but if it ain’t broke, don’t fix it!

Posted by admica   @   25 August 2009

Related Posts

Like this post? Share it!

Digg Twitter StumbleUpon Delicious Technorati Facebook RSS

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

Previous Post
« How to really delete TWiki attachments from the Trash
Next Post
Running bash shell scripts in debug mode to trace execution »
Powered by Wordpress   |   Lunated designed by ZenVerse