Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- section .bss
- num1 resb 2
- num2 resb 2
- res resb 2
- op resb 2
- section .data
- equal db ' = '
- nwln db 0xa
- section .text
- global _start
- _start:
- Loop:
- ; Read First Number
- mov eax, 3 ; System Call (Read)
- mov ebx, 0 ; File Descriptor (stdin)
- mov ecx, num1 ; Move data into 'num1'
- mov edx, 2
- int 0x80 ; Call Kernel
- ; Check for '.' to end the loop
- mov al, [num1]
- cmp al, '.'
- je End
- ; Read Operator
- mov eax, 3 ; System Call (Read)
- mov ebx, 0 ; File Descriptor (stdin)
- mov ecx, op ; Move data into 'op'
- mov edx, 2
- int 0x80 ; Call Kernel
- ; Read Second Number
- mov eax, 3 ; System Call (Read)
- mov ebx, 0 ; File Descriptor (stdin)
- mov ecx, num2 ; Move data into 'num2'
- mov edx, 2
- int 0x80 ; Call Kernel
- ; Check operator
- mov al, [op]
- cmp al, '+'
- je Plus
- cmp al, '-'
- je Minus
- cmp al, '*'
- je Multiply
- cmp al, '/'
- je Divide
- ;------------------------------------------------------------;
- Plus:
- call Addition
- jmp End
- Addition:
- ; Move x and y to registry
- mov ax, [num1]
- sub ax, '0'
- mov bx, [num2]
- sub bx, '0'
- ; Add ebx to eax
- add ax, bx
- add ax, '0'
- ; Move value in eax to [res]
- mov [res], ax
- call Print
- ret
- ;------------------------------------------------------------;
- Minus:
- call Subtraction
- jmp End
- Subtraction:
- ; Move x and y to registry
- mov ax, [num1]
- sub ax, '0'
- mov bx, [num2]
- sub bx, '0'
- ; Subtract ebx from eax
- sub ax, bx
- add ax, '0'
- ; Move value in eax to [res]
- mov [res], ax
- call Print
- ret
- ;------------------------------------------------------------;
- Multiply:
- call Multiplication
- jmp End
- Multiplication:
- ; Move x and y to registry
- mov ax, [num1]
- sub ax, '0'
- mov bx, [num2]
- sub bx, '0'
- ; Multiply ax with bx
- mul bx
- add ax, '0'
- ; Move value in eax to [res]
- mov [res], ax
- call Print
- ret
- ;------------------------------------------------------------;
- Divide:
- call Division
- ;jmp End
- Division:
- ; Move x and y to registry
- mov al, [num1]
- sub al, '0'
- mov bl, [num2]
- sub bl, '0'
- ; Multiply ax with bx
- div bl
- add al, '0'
- ; Move value in eax to [res]
- mov [res], ax
- call Print
- ret
- ;------------------------------------------------------------;
- Print:
- ; Write First Number
- mov eax, 4 ; System Call (Write)
- mov ebx, 1 ; File Descriptor (stdout)
- mov ecx, num1 ; Write 'num1'
- mov edx, 2
- int 0x80 ; Call Kernel
- ; Write Operator
- mov eax, 4 ; System Call (Write)
- mov ebx, 1 ; File Descriptor (stdout)
- mov ecx, op ; Write 'op'
- mov edx, 2
- int 0x80 ; Call Kernel
- ; Write Second Number
- mov eax, 4 ; System Call (Write)
- mov ebx, 1 ; File Descriptor (stdout)
- mov ecx, num2 ; Write 'num2'
- mov edx, 1
- int 0x80 ; Call Kernel
- ; Write Equal Sign
- mov eax, 4 ; System Call (Write)
- mov ebx, 1 ; File Descriptor (stdout)
- mov ecx, equal ; Write 'equal'
- mov edx, 3
- int 0x80 ; Call Kernel
- ; Write Result
- mov eax, 4 ; System Call (Write)
- mov ebx, 1 ; File Descriptor (stdout)
- mov ecx, res ; Write 'res'
- mov edx, 1
- int 0x80 ; Call Kernel
- ;New Line
- mov eax, 4 ; System Call (Write)
- mov ebx, 1 ; File Descriptor (stdout)
- mov ecx, nwln ; Write ' '
- mov edx, 1
- int 0x80 ; Call Kernel
- jmp Loop
- ;------------------------------------------------------------;
- End:
- mov eax,1 ; System Call (sys_exit)
- int 0x80 ; Call Kernel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement