Adding 2 and 3 digit capacity for assembly program

In summary, the conversation is discussing a program that uses subroutines to demonstrate multiplication. The program prompts the user for two factors, converts them to integers, and then multiplies them using the MULT subroutine. The result is displayed using the ITOA subroutine. The conversation also includes information about ASCII correction and the memory area. The speaker is asking for help with adding two-digit input and three-digit output capacity to the program.
  • #1
md01
2
0
I have the following program. I cannot figure out how to Add two-digit input and three-digit output capacity to it though.
; This program is designed to demonstrate the use of subroutines in
; programming.
; This file is phase 3 and introduces a multiply subroutine.
; Current support subroutines include : PMPTOUT, MULT, ATOI, ITOA

.ORIG x3000 ; load the program here

; Request factor1 from the user
; To be retrieved from R0 according to convention

AND R0,R0,#0

; Call prompt subroutine and output PROMPT1

LEA R3,PROMPT1
JSR PMPTOUT

TRAP x20 ; Use GETC to retrieve user character entry into R0
TRAP x21 ; Echo the entered factor

; Convert factor1 to an integer
; Use R2 to convert ASCII factor1 to an integer, store in R4 when done

AND R2,R2,#0 ; Clear R2
ADD R2,R0,#0 ; Load R2 with the ASCII value of factor1
JSR ATOI
ADD R4,R4,R2

LD R0,NewLine ; Go to a clean line for the next input
TRAP x21

; Request factor2 from user
; To be retrieved from R0 according to convention

AND R0,R0,#0

; Call prompt subroutine and output PROMPT2

LEA R3,PROMPT2
JSR PMPTOUT

TRAP x20 ; Use GETC to retrieve user character entry into R0
TRAP x21 ; Echo the entered factor

; Convert factor2 to an integer
; Use R2 to convert ASCII factor2 to an integer, store in R5 when done

AND R2,R2,#0 ; Clear R2
ADD R2,R0,#0 ; Load R2 with the ASCII value of factor1
JSR ATOI
ADD R5,R5,R2

LD R0,NewLine ; Go to a clean line for the next input
TRAP x21

; Perform multiplication with MULT subroutine
; Registers R4 and R5 correctly contain two integer factors here.
; R6 contains the integer product.

JSR MULT

; Use prompt writing subroutine to announce result

LEA R3,RESULT
JSR PMPTOUT

; Display the result of the multiplication on a clean line

JSR ITOA
TRAP x21 ; Write the product to the display
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
TRAP x25 ; HALT the processor
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PMPTOUT ST R7,SaveR7
AND R0,R0,#0
ADD R0,R3,R0
TRAP x22 ; Call PUTS
LD R7,SaveR7
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Multiply subroutine
; Expects factor1 in R4 and factor2 in R5
; Returns the product in R6

MULT AND R6,R6,#0 ; Clear R6 to hold result of multiplication
AGAIN ADD R6,R6,R4
ADD R5,R5,#-1
BRp AGAIN
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Subroutine to convert ASCII character to an integer
; This routine uses R2 as a holding register for the correction
ATOI ST R3,SaveR3
AND R3,R3,#0 ; Clear R3
LD R3,ASCII ; Load ASCII correction into R3
NOT R3,R3 ; Form 1s complement
ADD R3,R3,#1 ; Form 2s complement
ADD R2,R2,R3 ; ACSII correction, R2 now contains integer for
; math operations
LD R3,SaveR3
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
ITOA ST R3,SaveR3
AND R0,R0,#0 ; Clear R0 for product display
ADD R0,R6,R0 ; Load product integer into R0
AND R3,R3,#0 ; Clear R3 to ready ASCII correction
LD R3,ASCII ; Load ASCII correction into R3
ADD R0,R0,R3 ; Make integer an ASCII character for output
LD R3,SaveR3
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; MEMORY AREA

NewLine .FILL x000A
ASCII .FILL x0030
SaveR7 .FILL x0000
SaveR3 .FILL x0000
PROMPT1 .STRINGZ "Enter the first factor\n"
PROMPT2 .STRINGZ "Enter the second factor\n"
RESULT .STRINGZ "The product of the factors is :\n"

.END
; End of
 
Technology news on Phys.org
  • #2
Which CPU are you trying to program? I do not recognize the mnemonics.
 
  • #3
md01 said:
I have the following program. I cannot figure out how to Add two-digit input and three-digit output capacity to it though.
Helpful information would be what kind of assembly language you're using. This appears to me to be Motorola 6800 or 68000, but it could possibly be MIPS.

By "two digits" do you mean two decimal digits? If so, two decimal digits will fit into a byte, and three digits for your output will fit comfortably in two bytes.
md01 said:
; This program is designed to demonstrate the use of subroutines in
; programming.
; This file is phase 3 and introduces a multiply subroutine.
; Current support subroutines include : PMPTOUT, MULT, ATOI, ITOA

.ORIG x3000 ; load the program here

; Request factor1 from the user
; To be retrieved from R0 according to convention

AND R0,R0,#0

; Call prompt subroutine and output PROMPT1

LEA R3,PROMPT1
JSR PMPTOUT

TRAP x20 ; Use GETC to retrieve user character entry into R0
TRAP x21 ; Echo the entered factor

; Convert factor1 to an integer
; Use R2 to convert ASCII factor1 to an integer, store in R4 when done

AND R2,R2,#0 ; Clear R2
ADD R2,R0,#0 ; Load R2 with the ASCII value of factor1
JSR ATOI
ADD R4,R4,R2

LD R0,NewLine ; Go to a clean line for the next input
TRAP x21

; Request factor2 from user
; To be retrieved from R0 according to convention

AND R0,R0,#0

; Call prompt subroutine and output PROMPT2

LEA R3,PROMPT2
JSR PMPTOUT

TRAP x20 ; Use GETC to retrieve user character entry into R0
TRAP x21 ; Echo the entered factor

; Convert factor2 to an integer
; Use R2 to convert ASCII factor2 to an integer, store in R5 when done

AND R2,R2,#0 ; Clear R2
ADD R2,R0,#0 ; Load R2 with the ASCII value of factor1
JSR ATOI
ADD R5,R5,R2

LD R0,NewLine ; Go to a clean line for the next input
TRAP x21

; Perform multiplication with MULT subroutine
; Registers R4 and R5 correctly contain two integer factors here.
; R6 contains the integer product.

JSR MULT

; Use prompt writing subroutine to announce result

LEA R3,RESULT
JSR PMPTOUT

; Display the result of the multiplication on a clean line

JSR ITOA
TRAP x21 ; Write the product to the display
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
TRAP x25 ; HALT the processor
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PMPTOUT ST R7,SaveR7
AND R0,R0,#0
ADD R0,R3,R0
TRAP x22 ; Call PUTS
LD R7,SaveR7
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Multiply subroutine
; Expects factor1 in R4 and factor2 in R5
; Returns the product in R6

MULT AND R6,R6,#0 ; Clear R6 to hold result of multiplication
AGAIN ADD R6,R6,R4
ADD R5,R5,#-1
BRp AGAIN
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Subroutine to convert ASCII character to an integer
; This routine uses R2 as a holding register for the correction
ATOI ST R3,SaveR3
AND R3,R3,#0 ; Clear R3
LD R3,ASCII ; Load ASCII correction into R3
NOT R3,R3 ; Form 1s complement
ADD R3,R3,#1 ; Form 2s complement
ADD R2,R2,R3 ; ACSII correction, R2 now contains integer for
; math operations
LD R3,SaveR3
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
ITOA ST R3,SaveR3
AND R0,R0,#0 ; Clear R0 for product display
ADD R0,R6,R0 ; Load product integer into R0
AND R3,R3,#0 ; Clear R3 to ready ASCII correction
LD R3,ASCII ; Load ASCII correction into R3
ADD R0,R0,R3 ; Make integer an ASCII character for output
LD R3,SaveR3
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; MEMORY AREA

NewLine .FILL x000A
ASCII .FILL x0030
SaveR7 .FILL x0000
SaveR3 .FILL x0000
PROMPT1 .STRINGZ "Enter the first factor\n"
PROMPT2 .STRINGZ "Enter the second factor\n"
RESULT .STRINGZ "The product of the factors is :\n"

.END
; End of
 
  • #4
its using a LC3 calculator in MIPS
 
  • #5
md01 said:
its using a LC3 calculator in MIPS
The MIPS instruction set has two instructions for multiplication: MULT (signed multiplication) and MULTU (unsigned version). Why aren't you using one of these instructions instead of writing a subroutine?

Also, all of the MIPS registers are 32 bits (four bytes). What are the specific problems you're having with input and output?
 

1. How do I add two numbers with two digits in assembly language?

To add two numbers with two digits in assembly language, you can use the ADD instruction. First, you need to store the two numbers in separate registers. Then, use the ADD instruction to add the values in the two registers. Finally, store the result in a third register or in memory.

2. Can I add two numbers with three digits in assembly language?

Yes, you can add two numbers with three digits in assembly language. The process is similar to adding two numbers with two digits. You just need to use three registers to store the numbers and use the ADD instruction to add them together.

3. How do I handle overflow when adding two numbers in assembly language?

To handle overflow when adding two numbers in assembly language, you can use the JC (Jump if Carry) instruction. This instruction checks if the result of the addition is too large to fit in the register. If it is, the program can then handle the overflow accordingly.

4. Can I add numbers with decimal places in assembly language?

Yes, you can add numbers with decimal places in assembly language. However, assembly language does not have built-in instructions for handling floating-point numbers. You will need to implement a separate algorithm to handle decimal numbers, such as converting them to integers and then adding them.

5. Are there any limitations to adding numbers in assembly language?

As with any programming language, there may be limitations to adding numbers in assembly language. One limitation is the size of the registers, which can only hold a certain number of bits. This means that adding very large numbers may require more complex algorithms or multiple instructions. Another limitation is the lack of built-in support for floating-point numbers, as mentioned earlier. However, these limitations can often be overcome with careful programming and efficient algorithms.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top