Mark Pearl

So, in the coming days I am going to go through some more NASM examples. Today I am going to blog about some more basics of NASM. All of these items can be found in more detail at http://www.nasm.us/doc/nasmdoc3.html

Pseudo-instructions Assembly language has machine instructions and pseudo instructions. Pseudo-instructions contain commands to the assembler itself. Some examples in NASM include…

DB, DW, DD, DQ and DT (allocate storage space and initialise the space to specified values) REB, RESW, RESD, RESQ and REST (allocate storage space but do not initialise it) EQU (command for defining constants) TIMES (prefix for repeating instruction or data) Constants Four types of constants can be defined in NASM.

Numeric (integer, can be specified in binary, decimal, octal or hexadecimal representation) Character (consists of up to four characters enclosed in either singe or double quotes) String (looks like a character constant but may be longer than four characters) Floating Point (expressed in the following form: digits, then a period, then optionally more digits, then optionally an E followed by an exponent) Reserving Space for Data DB, DW and DD are used to reserve space and to initialise these storage points with values.

DB Define Byte DW Define Word DD Define Double Word

example:

db 0x55 ;Initialize 1 byte to 55h

db 0x55,0x56,0x57 ;Initialize 3 bytes to 55h, 56h & 57h

Declaring Uninitialized Data RESB, RESW and RESQ can also be used to reserve space but these pseudo-instructions do not initialize the reserved storage positions.

example:

buffer: resb 64 ;Reserve 64 bytes

wordvar: resw 1 ;Reserve one word

Defining a Symbolic Constant The instruction EQU can be used to assign a name to a given constant value. This is called a ‘symbolic constant’. For example…

message: db ‘hello’

msglen: equ $-message

main: mov ax, msglen

In the above example we use the symbol $. This stands for the current memory position of the message.

At first glance one would ask what is the difference between a label and a named constant. However, if you think about it a bit, a label is simply a point that you can jump to, while a named constant allows you to refer to a location in memory and perform operations on it without changing your “position”

Repeating Instructions The TIMES instruction causes the instruction immediately following it to be assembled a number of specific times. For example…

times 10 db ‘z’

will reserve 10 bytes and initialize these to the ascii character ‘z’.

Think of it as a way of repeating a line of code x number of times without having to type it out x number of times.

Local Labels A label starting with a period is treated as a local label, which means that it is associated with the previous non-local label.

label1:

.loop:

.

.

.

jne .loop

ret

label2:

.loop:

….

ret

So with this basic information in hand we should be able to write a program in NASM that is just a little more powerful than hello world.



blog comments powered by Disqus

Want to get my personal insights on what I learn as I learn it? Subscribe now!


/