SYS_EXIT equ 1 SYS_READ equ 3 SYS_WRITE equ 4 STDIN equ 0 STDOUT equ 1 section .data ; initialized data, messages welcome db 'Welcome to the Calculator',10,0 input1 db 'Enter first number:',0 input2 db 'Enter second number:',0 operators db '1. +', 10 db '2. -', 10 db '3. *', 10 db '4. /', 10, 0 choice db 'Your choice is: ',10,0 outcome db 'and the Result is: ',0 error db 'SORRY, Wrong choice!',10,0 nlinea db 10,0 section .bss ;uninitialized data ; Spaces reserved for storing the values provided by the user. opc: resb 2 ;reserves 2 byte in binary num1: resb 2 num2: resb 2 result: resb 2 section .text ;asm code global _start ;must be declared for using gcc _start: jmp main ; printstr print ascii string, which terminate with 0 byte ; input ; eax - point to begining of string ; return ; nothing printstr: push eax push ebx push ecx push edx push esi mov ecx, eax ; we store address of string in eax mov eax, SYS_WRITE mov ebx, STDOUT mov esi, ecx ; looking for end of stringS mov edx, 0 .0: cmp byte [esi], 0 je .1 inc edx inc esi jmp .0 .1: int 0x80 pop esi pop edx pop ecx pop ebx pop eax ret ; myexit terminate program ; input ; nothing ; output ; nothing myexit: mov eax, 1 mov ebx, 0 int 0x80 ; main function start here! main: mov eax, welcome call printstr mov eax, input1 call printstr ; We get num1 value for first number. mov eax, 3 mov ebx, 0 mov ecx, num1 mov edx, 2 int 80h mov eax, input2 call printstr ; We get num2 value for second number. mov eax, 3 mov ebx, 0 mov ecx, num2 mov edx, 2 int 80h mov al,[num2] sub al, '0' mov [num2],al mov al,[num1] sub al, '0' mov [num1],al mov eax, operators call printstr mov eax, choice call printstr ; Shows our choice mov eax,3 mov ebx,0 mov ecx,opc mov edx,2 int 80h mov ah, [opc] ; Move our choice to the 8bit ah register sub ah, '0' ; Convert from ascii to decimal ; We compare the value entered by the user to know what operation to perform. ; Basically, it is IF function (cmp) cmp ah, 1 je addition ;jump equal or jump zero cmp ah, 2 je subtraction cmp ah, 3 je multiplication cmp ah, 4 je division ; If the user choose wrong choice, it will show error and close porgram mov eax, error call printstr call myexit addition: ; We keep the numbers in the 8bit al and bl registers mov al, [num1] mov bl, [num2] ; Converts from ascii to decimal ;sub al, '0' ;sub bl, '0' ; Addition add al, bl ; Again converts from decimal to ascii add al, '0' ; We move the result mov [result], al ; We end our program jmp exit subtraction: ; We keep the numbers in 8bit al and bl registers mov al, [num1] mov bl, [num2] ; Convert from ascii to decimal ;sub al, '0' ;sub bl, '0' ; Subtraction sub al, bl ; Again converts from decimal to ascii add al, '0' ; We move the result mov [result], al ; We end our program jmp exit multiplication: ; We keep the numbers in 8bit al and bl registers mov al, [num1] mov bl, [num2] ; Convert from ascii to decimal ;sub al, '0' ;sub bl, '0' ; Multiplication. AX = AL x BL mul bl ; Again converts from decimal to ascii add ax, '0' ; We move the result mov [result], ax ; We end our program jmp exit division: ; We keep the numbers in 8bit al and bl registers mov al, [num1] mov bl, [num2] mov dx, 0 mov ah, 0 ; Convert from ascii to decimall ;sub al, '0' ;sub bl, '0' ; Division. AL = AX / BX div bl ; Again converts from decimal to ascii add ax, '0' ; We move the result mov [result], ax ; We end our program jmp exit exit: ; Prints 'and the Result is: ' mov eax, outcome call printstr ; Prints outcome mov eax, 4 mov ebx, 1 mov ecx, result mov edx, 1 int 80h ; Prints on screen two new lines mov eax, 4 mov ebx, 1 mov ecx, nlinea ; mov edx, lnlinea int 80h ; We end our program mov eax, 1 mov ebx, 0 int 80h
Comments
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎