Skip to content

Instantly share code, notes, and snippets.

@BigNerd95
Last active May 11, 2020 18:19
Show Gist options
  • Save BigNerd95/033df98e4a4451efb9f12e3009c97da3 to your computer and use it in GitHub Desktop.
Save BigNerd95/033df98e4a4451efb9f12e3009c97da3 to your computer and use it in GitHub Desktop.
; compile with
; nasm -f elf32 test_main.asm && gcc -o test_main -m32 test_main.o && ./test_main
section .data
msg db 'Elevazione a potenza', 13, 10, 0
base_str db 'Base: %d ', 13, 10, 0
esp_str db 'Esponente: %d', 13, 10, 0
res_str db 'Risultato: %d', 13, 10, 0
base dd 5
espo dd 10
section .text
global main
extern printf ; libc
main:
push EBP ; prologo
mov EBP, ESP ; prologo
push msg ; Argomento 1 (stringa formato)
call printf ; chiama printf (libc)
add ESP, 4 ; pulisce lo stack dai parametri (1 arg da 4 byte) (siamo in 32 bit)
mov EAX, [base] ; prende base dalla sezione dati
push EAX ; Argomento 2 (mette base sullo stack)
push base_str ; Argomento 1 (stringa formato)
call printf ; chiama printf
add ESP, 8 ; pulisce lo stack dai parametri (2 arg da 4 byte = 8 byte) (siamo in 32 bit)
mov EAX, [espo] ; prende esponente dalla sezione dati
push EAX ; Argomento 2 (mette esponente sullo stack)
push esp_str ; Argomento 1 (stringa formato)
call printf ; chiama printf (libc)
add ESP, 8 ; pulisce lo stack dai parametri (2 arg da 4 byte = 8 byte) (siamo in 32 bit)
mov EAX, [base] ; prende base dalla sezione dati
mov EBX, [espo] ; prende esponente dalla sezione dati
push EBX ; Argomento 2 (mette esponente sullo stack)
push EAX ; Argomento 1 (mette base sullo stack)
call eleva ; chiama eleva
ADD ESP, 8 ; pulisce lo stack dai parametri (2 par da 32 bit = 8 byte)
push EAX ; argomento 2 (in eax abbiamo il risultato di "eleva")
push res_str ; argomento 1 (stringa formato)
call printf ; chiama printf (libc)
add ESP, 8 ; pulisce lo stack dai parametri (2 arg da 4 byte = 8 byte) (siamo in 32 bit)
xor eax, eax ; tutto ok (risultato)
mov ESP, EBP ; epilogo
pop EBP ; epilogo
ret ; ritorno
; non ho usato exit perche altrimenti il buffer di printf non veniva flushato
eleva:
push EBP ; prologo
mov EBP, ESP ; prologo
mov ECX, [EBP + 12] ; Prende Argomento 2 (mette esponente in ECX)
mov EAX, 1 ; inizializza risultato a 1
test ECX, ECX ; controlla se l'esponente e' 0
jz exit ; se e' zero termina la routine
molt:
MUL dword [EBP + 8] ; moltiplica EAX per base
LOOP molt ; continua a moltiplicare finche ECX non e' 0
exit:
mov ESP, EBP ; epilogo
pop EBP ; epilogo
ret ; ritorno
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment