Variable Decleration in Assembly Language With Include Function Example Also

Posted by Suraj Singh on December 22, 2018 · 7 mins read
Hi readers,



Welcome Again To My Blog. I hope You all are Enjoying Latest Series of Assembly Tutorials. So, Today Again I am going To share Another piece of Code Of Assembly language Where I tried To Demonstrate How To Declare Variable And I Also Add Preview Post Examples Into This Script, Using include concept Available With Nasm Assembly Compiler.


Actually, In Nasm We Can Easily Append other Assembly Script Functionalities Easily Using %include syntax.
But Don't Worry Here, I Will Post Both Script.

By The Way, If you Are new And Don't Know How To Start, Compile Or Run?
Then, Don't Follow up Our Previous Blog post. Here, it's The List.

1. Hello World In Assembly
2. Find String Length In Assembly Language
3. Subroutine Demo In Assembly language
4. Some Subroutine Functionality Example
5. User Input Example In Assembly Language

Variable Decleration Example in Assembly Language


Main Script (VariableDecleration.asm)

;   Variable Decleration Example
;
; author : suraj singh bisht
; Email : surajsinghbisht054@gmail.com
;

; includable functions
%include 'includable_functions_without_start.asm'

; ====================================================
; [ Includable File]
; Functions:
; _strlen
; _exit
; _print
; _printline
;
; ====================================================
;

; bss section
SECTION .bss


; data section
SECTION .data
VName DB 'Hey! Suraj!', 0h ; Decleare String
VSingle DB 's' ; Allocate 1 Byte [Single]
VWord0 DW 1234 ; Allocate 2 Bytes [Word]
VWord1 DW -1234 ; Allocate 2 Bytes [Word]
VReal0 DD 1.1234 ; Allocate 4 Bytes [Double]
VReal1 DQ 1234.4567 ; Allocate 8 Bytes [Quadword]
VBignm DQ 123465789 ; Allocate 8 Bytes [Quadword]


; text section
SECTION .text
global _start ; linker trigger

; trigger function
_start:
; =================================================
; Note
; =================================================
; We can't directly print number on terminal in assembly.
; To print any digit, first we have to convert it into ASCII code.
; actually, assembly prints all character/Digit/Number/Word According
; To ASCII code Table.

; Let's print
mov eax, VName ; Load Memory address
call _printline ; call _printline Routine

; let's print anoter character
mov eax, VSingle ; Load Memory Address
call _printline ; Call _printline Routine

; Exit
jmp _exit ; Exit routine



;
; Output:
; Hey! Suraj!
; s�.��ˏ?U0*��I�@=�[
;
;


Another Script (includable_functions_without_start.asm)


; Assembly Includable File
; author : suraj singh bisht
; Email : surajsinghbisht054@gmail.com
;
; ====================================================
; [ Includable File]
; Functions:
; _strlen
; _exit
; _print
; _printline
;
; ====================================================





; Routine To Find String length
_strlen:
; Input
; EAX = address of string
; Output
; EAX = Length of String

; Initialize Registers
push ebx ; push ebx value into stack
mov ebx, 0 ; Set EBX = 0


; iteration Routine
_iterate:
cmp byte [eax], 0 ; Compare Last bit, if true, Set Zero flag = 1 Else Zero flag = 0
jz _return ; If Zero Flag = 1, Then Call function else pass
inc eax ; Move Memory Address Pointer
inc ebx ; Keep Record of Iteration
jmp _iterate ; Repeat

; Return String Calculated Length
_return:
mov eax, ebx ; Set Return Value EAX = EBX
pop ebx ; POP EBX Value
ret ; Return

; Exit Routine
_exit:
mov eax, 1 ; Invoke SYS_EXIT
mov ebx, 0 ; set Return Value Zero
int 80h ; interpt kernel


; Print Line
_print:
; Input
; EAX = addressof(string)
; Output
; Nothing

push ebx ; Store Registers Into Stack
push ecx
push edx
push eax

call _strlen ; Find String Len
mov edx, eax ; Set EDX= EAX {String Length}
mov eax, 4 ; invoke SYS_WRITE
mov ebx, 1 ; STDOUT
pop ecx ; POP String Address
int 80h ; interpt Kernel
pop edx ; POP all registers previous values
pop ecx ;
pop ebx ;
ret ; return

; PrintLine
_printline:
; Input
; EAX = addressof(string)
; Output
; nothing

call _print ; Print String

;
mov eax, 0 ; SET EAX = 0
push eax ; PUSH EAX Into Stack To make Sure; String End Character is 0

mov eax, 0AH ; New Line ASCII Code
push eax ; Push into The Stack

;
; Now Our Stack Look Like This
; -> -> -> -> ->
; ---------------------------------------------------
; | EAX[0AH] | EAX [0] | previous data |
; ---------------------------------------------------
; ESP EBP
;

mov eax, esp ; Set EAX = ESP
call _print ; call _print routine
pop eax ; Remove not useful pushed values from stack
pop eax ;
ret ; Return

Save Both Script Into Same Folder And Then Compile And Run VaribleDecleration.asm

have a nice day


Please Check Github For Latest Error Free Examples Codes. Click here