Skip to content

Instantly share code, notes, and snippets.

@MrElyazid
Last active January 19, 2024 21:13
Show Gist options
  • Save MrElyazid/89de2aa6f6c23d2120a30012fe63ebf0 to your computer and use it in GitHub Desktop.
Save MrElyazid/89de2aa6f6c23d2120a30012fe63ebf0 to your computer and use it in GitHub Desktop.
A simple calculator in 8086 assembly language, can be run using the program emu8086.
org 100h
jmp start
msg0: db "====================== CALCULATRICE EN 8086 ASSEMBLEUR =========================$",0dh,0ah
msg: db "+ _ Addition",0dh,0ah,"- _ Soustraction",0dh,0ah,"* _ Produit",0dh,0ah,"f _ Factoriel", 0Dh,0Ah,"p _ Puissance", 0Dh,0Ah,"/ _ Division", 0Dh,0Ah,"a _ Arrangement", 0Dh,0Ah,"c _ combinaison", 0Dh,0Ah,"v _ PGCD", 0Dh,0Ah, '$'
msg2: db 0dh,0ah,"Saisir un entier: $"
msg3: db 0dh,0ah,"Saisir un deuxieme entier: $"
msg4: db 0dh,0ah,"Choix non valide. ", 0dh,0ah,'$'
msg5: db 0dh,0ah,"Resultat : $"
msg6: db 0dh,0ah ,'appuyer ENTRER pour reafficher le menu, autre touhe pour quitter.', 0Dh,0Ah, '$'
msg_division_erreur: db 0dh,0ah, 'erreur de division $'
msg_erreur_arrangement: db "ne peut pas calculer l'arrangement quand k > n$"
msg_combination_invalid_k: db "ne peut pas calculer la combinaison quand k >n$"
msg_pgcd_erreur: db "le premier nombre doit etre superieur au deuxieme$"
tmp1 dw ?
tmp2 dw ?
tmp3 dw ?
start:
mov ah,9
mov dx, offset msg0
int 21h
mov ah,9
mov dx, offset msg ; afficher le premier message ( le menu )
int 21h
mov ah,0
int 16h ; attendre pour une touche de clavier, le code ascii est en al
cmp al,2Bh
je Addition
cmp al,2Dh
je soustraction
cmp al,2Ah
je produit
cmp al,66h
je factoriel
cmp al,70h
je puissance
cmp al,2Fh
je division
cmp al,61h
je arrangement
cmp al,63h
je combinaison
cmp al,76h
je pgcd
mov ah,09h
mov dx, offset msg4
int 21h
jmp start
InputNo: mov ah,0
int 16h
mov dx,0
mov bx,1 ; car on va multiplier par bx en FormNo.
cmp al,0dh
je FormNo
sub ax,30h ;convertir ascii en decimal
call ViewNo ;pour voir le nombre qu'on saisie
mov ah,0 ;besoin juste de la valeur en al
push ax ; empiler le nombre saisi
inc cx ;compteur des chiffres
jmp InputNo
FormNo: pop ax ; dernier nombre dans le stack
push dx ; dx est le nombre qu'on forme
mul bx ; on multiplie ax par une puissance de 10
pop dx
add dx,ax; on additionne le produit ax * puissance de 10 a notre nombre dx
mov ax,bx
mov bx,10
push dx
mul bx; ax = bx*10
pop dx
mov bx,ax; bx=bx*10
dec cx
cmp cx,0 ; cx est le nombre de chiffres qui compose le nombre souhaite
jne FormNo
ret
View: mov ax,dx ; cette procedure affiche le nombre en dx
mov dx,0
div cx
call ViewNo
mov bx,dx
mov dx,0
mov ax,cx
mov cx,10
div cx
mov dx,bx
mov cx,ax
cmp ax,0
jne View
ret
ViewNo: push ax ; afficher le chiffre en al
push dx
mov dx,ax
add dl,30h
mov ah,2
int 21h
pop dx
pop ax
ret
exit: mov dx,offset msg6
mov ah, 9
int 21h
mov ah,0
int 16h
cmp al,0dh
je reafficher
mov ah, 4Ch
int 21h
reafficher:
mov AX, 03h
int 10h
jmp start
ret
Addition: mov ah,9
mov dx, offset msg2
int 21h
mov cx,0
call InputNo
push dx
mov ah,9
mov dx, offset msg3
int 21h
mov cx,0
call InputNo
pop bx
add dx,bx
push dx
mov ah,9
mov dx, offset msg5
int 21h
mov cx,10000
pop dx
call View
jmp exit
produit: mov ah,9
mov dx, offset msg2
int 21h
mov cx,0
call InputNo
push dx
mov ah,9
mov dx, offset msg3
int 21h
mov cx,0
call InputNo
pop bx
mov ax,dx ; le dernier nombre entre est en dx, premier nombre en bx
mul bx
mov dx,ax
push dx
mov ah,9
mov dx, offset msg5
int 21h
mov cx,10000
pop dx
call View
jmp exit
soustraction: mov ah,9
mov dx, offset msg2
int 21h
mov cx,0
call InputNo
push dx
mov ah,9
mov dx, offset msg3
int 21h
mov cx,0
call InputNo
pop bx
sub bx,dx
mov dx,bx
push dx
mov ah,9
mov dx, offset msg5
int 21h
mov cx,10000
pop dx
call View
jmp exit
division: mov ah,9
mov dx, offset msg2
int 21h
mov cx,0
call InputNo
push dx
mov ah,9
mov dx, offset msg3
int 21h
mov cx,0
call InputNo
pop bx
cmp dx, 0
je div_zero
mov ax,bx
mov cx,dx
mov dx,0
div cx
mov dx,ax
push dx
mov ah,9
mov dx, offset msg5
int 21h
mov cx,10000
pop dx
call View
jmp exit
div_zero:
mov dx, offset msg_division_erreur
mov ah, 9
int 21h
jmp exit
puissance: ; la base en premier
mov ah,9
mov dx, offset msg2
int 21h
mov cx,0
call InputNo
push dx
mov ah,9
mov dx, offset msg3
int 21h
mov cx,0
call InputNo
pop bx
mov cx, dx
mov ax, 1
mov dx, 0000
etqp:
mul bx
loop etqp
push ax
mov ah, 9
mov dx, offset msg5
int 21h
pop dx
mov cx, 10000
call View
jmp exit
factoriel:
; methode classique de loop mul cx
mov ah, 9
mov dx, offset msg2
int 21h
mov cx, 0
call InputNo
mov cx, dx
mov ax, 0001
mov dx, 0000
etq:
mul cx
loop etq
mov dx, ax
push dx
mov ah, 9
mov dx, offset msg5
int 21h
mov cx, 10000
pop dx
call View
jmp exit
arrangement:
;premier entier n
mov ah, 9
mov dx, offset msg2
int 21h
mov cx, 0
call InputNo
push dx
; deuxieme entier k
mov ah, 9
mov dx, offset msg3
int 21h
mov cx, 0
call InputNo
push dx
pop bx ; bx est k
pop ax ; ax est n
cmp bx, ax ; verifier si k > n
jg erreur_arrangement
push ax
sub ax, bx
mov bx,ax
pop ax ; maintenant ax = n, bx = n-k
; calcul de n!
mov cx, ax
mov ax, 0001
mov dx, 0000
etqf:
mul cx
loop etqf
mov tmp1, ax
; calcul de (n-k)!
mov cx, bx
mov ax, 0001
mov dx, 0000
etqf1:
mul cx
loop etqf1
mov tmp2, ax
; Calcul de n! / (n-k)!
mov ax, tmp1
mov bx, tmp2
div bx
mov dx, ax
push dx
mov ah, 9
mov dx, offset msg5
int 21h
mov cx, 10000
pop dx
call View
jmp exit
erreur_arrangement:
mov ah, 9
mov dx, offset msg_erreur_arrangement
int 21h
jmp exit
combinaison:
; Get number of objects (n)
mov ah, 9
mov dx, offset msg2
int 21h
mov cx, 0
call InputNo
push dx
; Get number of chosen positions (k)
mov ah, 9
mov dx, offset msg3
int 21h
mov cx, 0
call InputNo
push dx
pop bx
pop ax
cmp bx, ax
jg erreur_combinaison
mov tmp1, bx ; tmp1 = p
push ax
sub ax,bx ; ax = n-p
mov bx, ax; bx = n-p
;pop ax; ax = n
mov cx, tmp1
mov ax, 0001
mov dx, 0000
etqf3:
mul cx
loop etqf3
mov tmp1, ax ; tmp1 = p!
mov cx,bx
mov ax, 0001
mov dx, 0000
etqf4:
mul cx
loop etqf4
mov tmp2, ax ; tmp2 = (n-p)!
pop cx ; cx = n
mov ax, 0001
mov dx, 0000
etqf5:
mul cx
loop etqf5
; ax = n! donc on divise deux fois et directement le resultat sera en ax
mov bx, tmp1
div bx ; division sur p!
mov bx, tmp2
div bx ; division sur (n-p)!
mov dx, ax
push dx
mov ah, 9
mov dx, offset msg5
int 21h
mov cx, 10000
pop dx
call View
jmp exit
erreur_combinaison:
mov dx, offset msg_combination_invalid_k
mov ah,9
int 21h
jmp exit
pgcd:
; premier nombre a
mov ah, 9
mov dx, offset msg2
int 21h
mov cx, 0
call InputNo
push dx
; deuxieme nombre b
mov ah, 9
mov dx, offset msg3
int 21h
mov cx, 0
call InputNo
push dx
pop bx ; bx = b
pop ax ; ax = a
cmp bx, ax
jg erreur_pgcd
euclid:
mov dx, 0
div bx
mov ax, bx
mov bx, dx
cmp bx, 0
je resultat
jmp euclid
resultat:
mov dx, ax
push dx
mov ah, 9
mov dx, offset msg5
int 21h
mov cx, 10000
pop dx
call View
jmp exit
erreur_pgcd:
mov dx, offset msg_pgcd_erreur
mov ah, 9
int 21h
jmp exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment