Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

NASM Assembly x86 Write a program that uses procedures to solve mathematical equations. The program terminates at a . Example input: 1+4 4-1 2*2 8/2

NASM Assembly x86

Write a program that uses procedures to solve mathematical equations. The program terminates at a .

Example input:

1+4

4-1

2*2

8/2

.

Example output:

1 + 4 = 5 4 - 1 = 3 2 * 2 = 4 8 / 2 = 4

here is my code so far

section .data inval: db 'invalid operator', 10 invallen: equ $-inval section .bss num1 resb 1 ind resb 1 num2 resb 1 op resb 1 counter resb 1 result resb 1 section .text global _start

_start:

mov [counter], byte 0

again: call readnum1 call readoperator call readnum2 call skip

cmp [num1], byte '.' jz end call display cmp [op], byte 43;compare operator to '+' jz addition ;if operator is a '+', then jump to addition block cmp [op],byte 47;compare operator to '/' jz divide ;if operator is a '/', then jump to division block cmp [op],byte 42;compare operator to '*' jz multiply;if operator is a '*', then jump to multiply block cmp [op],byte 45;compare operator to '-' jz subtract;if operator is a '-', jump to subtract block jmp invalid ;if none of the above jumps are made, jump to invalid block addition: mov eax, [num1] mov ebx, [num2] add eax, ebx sub eax, 48 mov [result], eax ;print result mov eax,4 ; The system call for write (sys_write) mov ebx,1 ; File descriptor 1 - standard output mov ecx,result ; Put the offset of hello in ecx mov edx,1 int 80h ; Call the kernel jmp again subtract: mov eax, [num1] mov ebx, [num2] sub eax, ebx add eax, 48 mov [result], eax ;print result mov eax,4 ; The system call for write (sys_write) mov ebx,1 ; File descriptor 1 - standard output mov ecx,result ; Put the offset of hello in ecx mov edx,1 int 80h ; Call the kernel jmp again multiply: mov ax, [num1] sub ax, word '0' mov bx, [num2] sub bx, word '0' mul bx add eax, 48 mov [result], eax ;print result mov eax,4 ; The system call for write (sys_write) mov ebx,1 ; File descriptor 1 - standard output mov ecx,result ; Put the offset of hello in ecx mov edx,1 int 80h ; Call the kernel jmp again divide: mov al, [num1] sub al, byte '0' mov bl, [num2] sub bl, byte '0' div bl add al, 48 mov [result], ax ;print result mov eax,4 ; The system call for write (sys_write) mov ebx,1 ; File descriptor 1 - standard output mov ecx,result ; Put the offset of hello in ecx mov edx,1 int 80h ; Call the kernel jmp again invalid:;display message mov eax,4 mov ebx,1 mov ecx,inval mov edx,invallen int 80h ; Call the kernel jmp end;end of jump jmp again

indent: mov eax, 3 mov ebx, 0 mov ecx, num1 mov edx, 1 int 80h ret

jmp end display: mov eax,4 mov ebx,1 mov ecx,num1 mov edx,1 int 80h ; Call the kernel mov eax,4 mov ebx,1 mov ecx,op mov edx,1 int 80h ; Call the kernel mov eax,4 mov ebx,1 mov ecx,num2 mov edx,1 int 80h ; Call the kernel ret jmp end readnum1:;read first number mov eax,3 ; The system call for write (sys_write) mov ebx,0 ; File descriptor 1 - standard output mov ecx,num1 ; Put the offset of hello in ecx mov edx,1 int 80h ; Call the kernel ret jmp end readoperator: ;read operator mov eax,3 ; The system call for write (sys_write) mov ebx,0 ; File descriptor 1 - standard output mov ecx,op ; Put the offset of hello in ecx mov edx,1 int 80h ; Call the kernel ret jmp end readnum2: ;read second number mov eax,3 ; The system call for write (sys_write) mov ebx,0 ; File descriptor 1 - standard output mov ecx,num2 ; Put the offset of hello in ecx mov edx,1 int 80h ; Call the kernel ret jmp end skip: mov eax, 3 mov ebx, 0 mov ecx, ind mov edx, 1 int 80h ret end: mov eax,1 ; The system call for exit (sys_exit) mov ebx,0 ; Exit with return code of 0 (no error) int 80h;

It doesn't seem to properly read the second line. I'm going to be quizzed on this tomorrow and need to understand how to make it work. Not too worried about how the display looks more concerned on how to get it to properly read the second line.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke, David Auer

11th Edition

B003Y7CIBU, 978-0132302678

More Books

Students also viewed these Databases questions