Before trying to get our hands on Pseudo-Instructions, lets first do a recap of last two post. After that we’ll try to look into more mnemonics before actually moving on to Assembly Language Programming. This will be a wild run so keep it tight, keep it together. :P
Till now we talked about x86 family of processors, their 16-bit registers and 32-bit registers, EAX, EBX, ECX and so on. Mode of operations in above processors i.e real mode and protected mode and little bit about the differences between them and then very little touch on Interrupts.
Then we moved on assembly language, how its better then machine language and basic instructions in assembly language i.e MOV, ADD, SUB, INC and DEC and how to comment in the ASM code. If you know exactly what i’m talking about then we are good to move forward if not then visit my previous post regarding assembly language.
So if you are still here then i can assume you know what i was talking about.
In that case lets move forward…
In this post we’ll learn about Pseudo-Instructions. This post will closely follow NASM and might not work with other assemblers so take it all with a pinch of salt.
Pseudo-instructions are not x86 instructions directly rather they are part of the NASM assembler (though are very common in other assemblers too.). These are used to declare initialized and uninitialized data and few other things. Lets look over them in brief :
Declaring Initialized Data
DB, DW, DD, DQ, DT, DDQ, DO are used to declare initialized data in the output file. D stands for data and rest are defined in following table:
general format for this is –
[Label] <Pseudo-Instruction> <value>
few ways to call these :
label1 db ABh ;declare initialized byte with initial
;value AB in hex with label label1
label2 db 1010010b ;declare initialized byte with initial
;value 1010010 in binary with label label2
label3 dw 12ABh ;declare initialized word with initial
;value 12AB in hex with label label3.
label4 dd 1A2Bh ;declare initialized double word with
;initial value 1A2B in hex with label
label5 db "A" ;declare initialized byte with initial
;value as ASCII code of A i.e. 65 in
Note : In assembly language double quotes and single quotes are same. so ‘A’ is same as “A”.
Till now we only declare single initialized byte or word or other now lets see how to initialize multiple at once. just like arrays in C/C++.
label6 db 0, 1, 2, 3 ;declare 4 consecutive bytes with values
;0, 1, 2 and 3 respectively.
label7 db "h", "e", "l", "l", "o", 0 ; declares 6 bytes to
;represents a null
;terminated string "hello"
;just like in C.
label8 db "hello",0 ; same as label7.
Note : To define a null terminated string in assembly it must contain 0 at the end.
Declaring Uninitialized Data
RESB, RESW, RESD, RESQ, REST, RESDQ are just like reinitialized data declaration just it only reserves the bytes and have only one operand that is no. of byte, word, double word etc to reserve. And you have guessed it right RES stands for reserve and fallowing latter represents byte, word etc same as above table. E.g.
mybuffer: resb 64 ; reserve 64 byte with label mybuffer.
mywordbuffer: resw 64 ;reserve 64 words with label
Including external binary file
INCBIN pseudo-instruction is used to include a binary file into the output file. this can be handy to include other binaries into the code like graphics. However its highly recommended not to use large pieces of binary. INCBIN can work with 1 to 3 operands where first one is the name of the binary file, second is the starting point and third being the max number of bytes to include. E.g.
incbin "myfile.dat" ;include the whole file
incbin "myfile.dat", 512 ;include the file myfile.dat but
;skip first 512 bytes.
incbin "myfile.dat", 512, 1024 ;include the file myfile.dat but
;skip first 512 bytes and include
;at most 1024 bytes.
To define constants just like in C/C++ we use EQU keyword or instruction. So just like C/C++ we cannot alter the value later during the execution. EQU actually defines a label name to the value and this definition is absolute. E.g.
message db 'hello, world'
msglen equ 12
The above instructions will create a message with ‘hello, world’ and msglen 12, msglen cannot be altered. Remember double quotes and single quotes both are same in assembly :).
Repeating Instructions or Data
The final pseudo-instruction for this tutorial is TIMES. As the heading suggest its used to do declare or reserve multiple times or repeatedly. you can treat it as while loop in C/C++. time is very versatile instruction it can iterate constant number of times or till a condition fulfills. E.g.
buffer: TIMES 64 db 0 ;declare a buffer of 64 bytes with
;initial value as 0.
In above statement db 0 will run 64 times, but as i said times is much more versatile you can pass other instructions as well. E.g.
TIMES 64 ADD EAX,1 ; this will increment EAX by 64
However you cannot pass macro (worry not will be covered later!) in TIMES as macros process well before the assembler executes TIMES.
NOTE : if your using MASM then TIMES is not there use DUP (duplicate) instead.
Well this is enough for this time will learn about expressions and critical expressions.
As always thank you for reading.