Assembly Language Program - Perform Math Operations

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 16067 Views - Last Post: 02 April 2012 - 09:36 PM

#1 mikex6989  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 101
  • Joined: 18-February 11

Assembly Language Program - Perform Math Operations

Posted 27 March 2012 - 03:00 PM

I am an assembly program newbie. I have to write an assembly language program that does the following, but I do not know where to start. Any help would be appreciated.


Write an assembly language program which allows the user to perform basic arithmetic. The program will display a menu to the user to:
1. Add
2. Subtract
3. Divide
4. Multiply
5. Exit
When the user selects the operation they should then be prompted for two integers. Perform the arithmetic operation and print the result. Continue displaying the menu after each operation until the user selects to exit the application. You must use procedures to:
1. display the menu and return the selection
2. perform the addition
3. perform the subtraction
4. perform the multiplication
5. perform the division

Is This A Good Question/Topic? 0
  • +

Replies To: Assembly Language Program - Perform Math Operations

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Assembly Language Program - Perform Math Operations

Posted 27 March 2012 - 03:17 PM

* Moved to the Assembly forum *
Was This Post Helpful? 0
  • +
  • -

#3 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,285
  • Joined: 28-March 11

Re: Assembly Language Program - Perform Math Operations

Posted 27 March 2012 - 03:19 PM

Drats, you moved it as I hit reply.

What have you tried? As we won't do your homework for you, you will have to show some effort on your part! What Processor? What OS? Which Assembler?
Was This Post Helpful? 0
  • +
  • -

#4 mikex6989  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 101
  • Joined: 18-February 11

Re: Assembly Language Program - Perform Math Operations

Posted 27 March 2012 - 07:08 PM

Like I said I'm a newbie..I don't know where to begin..How do I get input?
Was This Post Helpful? 0
  • +
  • -

#5 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,285
  • Joined: 28-March 11

Re: Assembly Language Program - Perform Math Operations

Posted 27 March 2012 - 07:37 PM

Quote

What have you tried? As we won't do your homework for you, you will have to show some effort on your part! What Processor? What OS? Which Assembler?

Was This Post Helpful? 0
  • +
  • -

#6 mikex6989  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 101
  • Joined: 18-February 11

Re: Assembly Language Program - Perform Math Operations

Posted 27 March 2012 - 07:42 PM

.Model small
.Stack 10h
.Data

		 msg1 db "Enter number 1  :  "
		 msg2 db "Enter number 2  :  "

.Code

Task :
			  mov ax,@data
			  mov ds,ax

		   mov dx, offset msg1
		   mov ah,09h
			int 21h

		   mov ah, 01h
			int 21h


end Task


Was This Post Helpful? 0
  • +
  • -

#7 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,285
  • Joined: 28-March 11

Re: Assembly Language Program - Perform Math Operations

Posted 27 March 2012 - 07:45 PM

Oh fun, DOS interrupts.

http://www.cs.cmu.edu/~ralf/files.html

http://ctyme.com/rbrown.htm
Was This Post Helpful? 0
  • +
  • -

#8 mikex6989  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 101
  • Joined: 18-February 11

Re: Assembly Language Program - Perform Math Operations

Posted 28 March 2012 - 06:22 AM

I don't think this is what my program is asking. We've never talked about this in my class
Was This Post Helpful? 0
  • +
  • -

#9 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,285
  • Joined: 28-March 11

Re: Assembly Language Program - Perform Math Operations

Posted 28 March 2012 - 02:33 PM

Quote

We've never talked about this in my class

So, why would that stop you from learning it on your own, or researching the information? A major job of any programmer in any language is problem solving.

Since you are using DOS, download the interupt list part a to f from here It contains all the interupt you will ever need to program in DOS and then some.

Create a menu in your data section, something like:

szMenu          db  "What do you want to do?  Type number of your selection", 13, 10
                db  "1. Add", 13, 10
                db  "2. Subtract", 13 ,10, 0
etc...
display the menu with int21/ah=9

Enter a loop and call int21/ah=8 check the return value, if it is a menu choice jump to code label that will take care of the selection, if not valid menu, keep looping.

Read the Intel and/or AMD manuals for instructions for ADD, SUB, MUL, DIV.
Was This Post Helpful? 0
  • +
  • -

#10 mikex6989  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 101
  • Joined: 18-February 11

Re: Assembly Language Program - Perform Math Operations

Posted 02 April 2012 - 04:17 PM

I am having trouble with something. I want the user to enter A, S, D, M, E to either add, sub, divide, etc...How can I say if readString == A then enter integer 1 and integer 2 and add?

Here is my code so far:


TITLE MASM Template						(main.asm)

INCLUDE Irvine32.inc
.data


myMenu BYTE 'Welcome! Please choose either of the following operations:', 13,10,0
myAdd BYTE 'Add (A)',13,10,0
mySub BYTE 'Subtract (S)',13,10,0
myDiv BYTE 'Divide (D)',13,10,0
myMult BYTE 'Multiply (M)',13,10,0
myExit BYTE 'Exit (E)',13,10,0
myEnter BYTE 'Selection: ',13,10,0
addVar BYTE 'A',13,10,0


.code
main PROC
	call Clrscr

	mov edx,OFFSET myMenu
	call WriteString
	mov	 edx,OFFSET myAdd
	call WriteString
	mov edx,OFFSET mySub
	call WriteString
	mov edx,OFFSET myDiv
	call WriteString
	mov edx,OFFSET myMult
	call WriteString
	mov edx,OFFSET myExit
	call WriteString
	
	call crlf
	mov edx,OFFSET myEnter
	call WriteString
	call readString
	mov edx, OFFSET addVar

	exit
main ENDP

END main



Was This Post Helpful? 0
  • +
  • -

#11 mikex6989  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 101
  • Joined: 18-February 11

Re: Assembly Language Program - Perform Math Operations

Posted 02 April 2012 - 06:07 PM

could someone please offer some help? I am very confused. This is what I have so far:


TITLE MASM Template						(main.asm)

INCLUDE Irvine32.inc
.data


myMenu BYTE 'Welcome! Please choose either of the following operations:', 13,10,0
myAdd BYTE 'Add (A)',13,10,0
mySub BYTE 'Subtract (S)',13,10,0
myDiv BYTE 'Divide (D)',13,10,0
myMult BYTE 'Multiply (M)',13,10,0
myExit BYTE 'Exit (E)',13,10,0
myInput BYTE 'Selection: ',13,10,0

myInteger1 BYTE 'Enter Integer 1: ',13,10,0
myInteger2 BYTE 'Enter Integer 2: ',13,10,0

inpVar DWORD ?


.code
main PROC
	call Clrscr

	mov edx,OFFSET myMenu
	call WriteString
	mov	 edx,OFFSET myAdd
	call WriteString
	mov edx,OFFSET mySub
	call WriteString
	mov edx,OFFSET myDiv
	call WriteString
	mov edx,OFFSET myMult
	call WriteString
	mov edx,OFFSET myExit
	call WriteString
	
	call crlf					; new line
	mov edx,OFFSET myInput
	call WriteString
	call ReadString
	mov inpVar, ReadString
	cmp inpVar, 'A'
	je Equal
	jmp L2

	Equal:
	mov edx,OFFSET myInteger1
	call WriteString
	call ReadString

	mov edx,OFFSET myInteger2
	call WriteString
	call ReadString

	L2:




	exit
main ENDP

END main



This post has been edited by mikex6989: 02 April 2012 - 06:11 PM

Was This Post Helpful? 0
  • +
  • -

#12 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,285
  • Joined: 28-March 11

Re: Assembly Language Program - Perform Math Operations

Posted 02 April 2012 - 07:13 PM

What happened to using DOS Interrupts?
What are you confused about?

Line 42 is wrong.
42 mov inpVar, ReadString

Read string returns the size of the input string in eax
And you have to pass it a pointer to a buffer in edx and the buffer length in ecx.

Maybe what you are after is ReadChar?
Was This Post Helpful? 0
  • +
  • -

#13 mikex6989  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 101
  • Joined: 18-February 11

Re: Assembly Language Program - Perform Math Operations

Posted 02 April 2012 - 07:27 PM

I don't understand why my jumps are not working properly. If I enter 1 I want to jump to L1 to perform the add operation.


TITLE MASM Template						(main.asm)

INCLUDE Irvine32.inc
.data


myMenu BYTE 'Welcome! Please choose either of the following operations:', 13,10,0
myAdd BYTE 'Add		(A)',13,10,0
mySub BYTE 'Subtract	(S)',13,10,0
myDiv BYTE 'Divide		(D)',13,10,0
myMult BYTE 'Multiply	(M)',13,10,0
myExit BYTE 'Exit		(E)',13,10,0
myInput BYTE 'What would you like to do? ',13,10,0

myInteger1 BYTE 'Enter Integer 1: ',13,10,0
myInteger2 BYTE 'Enter Integer 2: ',13,10,0

.code
main PROC
	call Clrscr

	mov edx,OFFSET myMenu
	call WriteString
	call crlf				; new line

	mov	 edx,OFFSET myAdd
	call WriteString
	mov edx,OFFSET mySub
	call WriteString
	mov edx,OFFSET myDiv
	call WriteString
	mov edx,OFFSET myMult
	call WriteString
	mov edx,OFFSET myExit
	call WriteString
	
	call crlf					; new line
	mov edx,OFFSET myInput
	call WriteString
	call ReadInt
	cmp ReadInt, 1
	je L1
	jmp L2

	L1:

	call crlf
	mov edx,OFFSET myInteger1
	call WriteString
	call ReadInt

	mov edx,OFFSET myInteger2
	call WriteString
	call ReadInt

	mov eax, 0
	add eax, OFFSET myInteger1
	add eax, OFFSET myInteger2

	jmp L2:


	exit
main ENDP

END main




i also don't know how i would display the added integers afterwards.
Was This Post Helpful? 0
  • +
  • -

#14 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,285
  • Joined: 28-March 11

Re: Assembly Language Program - Perform Math Operations

Posted 02 April 2012 - 07:32 PM

you call ReadInt and then you compare ReadInt to 1. No, no. ReadInt returns a value in eax cmp eax NOT an address of a proc
Was This Post Helpful? 0
  • +
  • -

#15 mikex6989  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 101
  • Joined: 18-February 11

Re: Assembly Language Program - Perform Math Operations

Posted 02 April 2012 - 07:35 PM

isn't that what I am doing here?

	call crlf					; new line
	mov edx,OFFSET myInput
	call WriteString
	call ReadInt
	cmp ReadInt, 1
	je L1
	jmp L2

	L1:

	call crlf
	mov edx,OFFSET myInteger1
	call WriteString
	call ReadInt

	mov edx,OFFSET myInteger2
	call WriteString
	call ReadInt

	mov eax, 0
	add eax, OFFSET myInteger1
	add eax, OFFSET myInteger2
	

	L2:



even if its not 1 it is jumping to L1
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2