; The code below will print the message „Ana has 17 apples”
bits 32
global start
; declare extern functions used by the program
extern exit, printf ; add printf as extern function
import exit msvcrt.dll
import printf msvcrt.dll ; tell the assembler that function printf can be found in library msvcrt.dll
segment data use32 class=data
; char arrays are of type byte
format db "Ana has %d apples", 0 ; %d will be replaced with a number
; char strings for C functions must terminate with 0
segment code use32 class=code
start:
mov eax, 17
; will call printf(format, 17) => will print: „Ana has 17 apples”
; place parameters on the stack from right to left
push dword eax
push dword format ; ! on the stack is placed the address of the string, not its value
call [printf] ; call function printf for printing
add esp, 4 * 2 ; free parameters on the stack; 4 = size of dword; 2 = number of parameters
; exit(0)
push dword 0 ; push on stack the parameter for exit
call [exit] ; call exit to terminate the programme
Laboratory 8 - Examples
Function calls
Attention when using scanf function
Pay attention to the representation of the variables according to the format used.
See table from the section Standard msvcrt functions from the theory.
See the following WRONG example (this is a frequent mistake) and try to understand what happens:
; The following program should read a number and print the message together with the number on the screen.
bits 32
global start
; declaring extern functions used in the program
extern exit, printf, scanf
import exit msvcrt.dll
import printf msvcrt.dll ; indicating to the assembler that the printf fct can be found in the msvcrt.dll library
import scanf msvcrt.dll ; similar for scanf
segment data use32 class=data
n db 0 ; this is the variable where we store the number read from keyboard
message db "Numarul citit este n= %d", 0
format db "%d", 0 ; %d <=> a decimal number (base 10)
segment code use32 class=code
start:
; calling scanf(format, n) => we read the number and store it in the variable n
; push parameters on the stack from right to left
push dword n ; ! address of n, not the value
push dword format
call [scanf] ; call scanf for reading
add esp, 4 * 2 ; taking parameters out of the stack; 4 = dimension of a dword; 2 = nr of parameters
;convert n to dword for pushing its value on the stack
mov eax,0
mov al,[n]
;print the message and the value of n
push eax
push dword message
call [printf]
add esp,4*2
; exit(0)
push dword 0 ; push the parameter for exit on the stack
call [exit] ; call exit
In the previous example the beginning of the message is overwritten when we read the number n, hence nothing is printed on the screen.
Printing a message
Printing a quadword on the screen
segment data use32 class=data
a dq 300765694775808
format db '%lld',0
segment code use32 class=code
start:
push dword [a+4]
push dword [a]
push dword format
call [printf]
add esp,4*3
push dword 0
call [exit]
Reading a number from keyboard
; The code below will print message ”n=”, then will read from keyboard the value of perameter n.
bits 32
global start
; declare extern functions used by the programme
extern exit, printf, scanf ; add printf and scanf as extern functions
import exit msvcrt.dll
import printf msvcrt.dll ; tell the assembler that function printf is found in msvcrt.dll library
import scanf msvcrt.dll ; similar for scanf
segment data use32 class=data
n dd 0 ; in this variable we'll store the value read from the keyboard
; char strings are of type byte
message db "n=", 0 ; char strings for C functions must terminate with 0(value, not char)
format db "%d", 0 ; %d <=> a decimal number (base 10)
segment code use32 class=code
start:
; will call printf(message) => will print "n="
; place parameters on stack
push dword message ; ! on the stack is placed the address of the string, not its value
call [printf] ; call function printf for printing
add esp, 4*1 ; free parameters on the stack; 4 = size of dword; 1 = number of parameters
; will call scanf(format, n) => will read a number in variable n
; place parameters on stack from right to left
push dword n ; ! addressa of n, not value
push dword format
call [scanf] ; call function scanf for reading
add esp, 4 * 2 ; free parameters on the stack
; 4 = size of a dword; 2 = no of perameters
; exit(0)
push dword 0 ; place on stack parameter for exit
call [exit] ; call exit to terminate the program
Saving register values before function calls
; The code below will calculate the result of some arithmetic operations in the EAX register, save the value of the registers, then display the result value and restore the value of the registers.
bits 32
global start
; declare extern functions
extern exit, printf
import exit msvcrt.dll
import printf msvcrt.dll ; tell assembler function is found in library msvcrt.dll
segment data use32 class=data
; string of bytes
format db "%d", 0 ; %d <=> decimal number
segment code use32 class=code
start:
; will calculate 20 + 123 + 7 in EAX
mov eax, 20
add eax, 123
add eax, 7 ; eax = 150 (base 10) or 0x96 (base 16)
; save the value of the registers because printf function call will modify their values
; use instruction PUSHAD which saves on stack the values of several registers: EAX, ECX, EDX and EBX
; in this example the value of EAX must be saved, but the instruction can be generally applied
PUSHAD
; will call printf(format, eax) => will print value from eax
; place parameters on stack from right to left
push dword eax
push dword format ; ! address of string on stack, not value
call [printf] ; call function for printing
add esp, 4*2 ; free parameters on the stack; 4 = size of dword; 2 = number of parameters
; after function call printf EAX register has a value set by this function (not the value 150 we have computer at the beginning of the program)
; restore value of registers saved on stack using POPAD
; this instruction takes values from the stack and restores them in several registers: EAX, ECX, EDX and EBX
; it is important that before calling instruction POPAD we make sure there are enough values on stack
; to be placed in registers (for example make sure that PUSHAD was called before)
POPAD
; now value from EAX is restored to its previous value just before calling PUSHAD (in this case, value 150)
; exit(0)
push dword 0 ; we place on stack parameter for exit
call [exit] ; call exit to end the program
Text files operations
Create a new file
ASM file: create_file.asm
Write a text in a new file
ASM file: create_write_file.asm
Append a text in a new file
ASM file: create_append_file.asm
Read a short text (maximum 100 characters) from a file
ASM file: read_short_text_from_file_no_display.asm
Read a short text (maximum 100 caractere) from a file and display it
Read the whole text from a file (in stages)
ASM File: read_full_file.asm