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 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]
		

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

ASM file: read_short_text_from_file_and_display.asm

Read the whole text from a file (in stages)

ASM File: read_full_file.asm