Assembler lanaguage program
The program is to prompt the user to enter a number between 2 and 100. Reject any invalid user inputs and terminate the program if invalid inputs are entered. Print each even number from 2 to the user entered number. Sum these even numbers and print the sum. Print each odd number from 1 to the user entered number. Sum these odd numbers and print the sum. Exit the program when the output is complete.
The output of your program should look something like this:
Enter an integer between 1 and 100.
You entered 10.
The even numbers from 1 to 10 are:
2
4
6
8
10
The even sum is: 30
The odd numbers from 1 to 10 are:
1
3
5
7
9
The odd sum is: 25
Here is the code:
.text
main:
la $a0, user #ask the user for input
li $v0, 4
syscall
li $v0, 5 #syscall for reading user input
syscall
move $s0, $v0 #$s0 will have the user input now
la $a0, output #print the input from user
li $v0, 4
syscall
move $a0, $s0
li $v0, 1
syscall
la $a0, endl
li $v0, 4
syscall
blt $s0, 2, quit #check if within limits
bgt $s0, 100, quit
li $s1, 2 #this will iterate from 2 to input
li $s2, 0
la $a0, evenNum
li $v0, 4
syscall
move $a0, $s0
li $v0, 1
syscall
la $a0, endl
li $v0, 4
syscall
eveniter:
move $a0, $s1 #print current number
li $v0, 1
syscall
la $a0, endl
li $v0, 4
syscall
add $s2, $s2, $s1 #add into the sum
addi $s1, $s1, 2
bgt $s1, $s0, doneeven #if user input is reached then done
j eveniter #continue iterating otherwise
doneeven:
la $a0, evenSum #print the sum
li $v0, 4
syscall
move $a0, $s2
li $v0, 1
syscall
la $a0, endl
li $v0, 4
syscall
li $s1, 1 #now odd numbers
li $s2, 0
la $a0, oddNum
li $v0, 4
syscall
move $a0, $s0
li $v0, 1
syscall
la $a0, endl
li $v0, 4
syscall
odditer:
move $a0, $s1 #print the current number
li $v0, 1
syscall
la $a0, endl
li $v0, 4
syscall
add $s2, $s2, $s1 #add into the sum
addi $s1, $s1, 2
bgt $s1, $s0, doneodd #if user input is reached then done
j odditer #otherwise continue iterating
doneodd:
la $a0, oddSum #print the sum
li $v0, 4
syscall
move $a0, $s2
li $v0, 1
syscall
la $a0, endl
li $v0, 4
syscall
li $v0, 10 #exit the program
syscall
quit:
la $a0, errorMessage #print error message
li $v0, 4
syscall
li $v0, 10
syscall
.data
user: .asciiz "Enter an integer between 1 and 100: "
output: .asciiz "You entered "
endl: .asciiz "\n"
evenNum: .asciiz "The evenNum numbers from 1 to "
oddNum: .asciiz "The oddNum numbers from 1 to "
are: .asciiz "are:\n"
evenSum: .asciiz "The evenNum sum is: "
oddSum: .asciiz "The oddNum sum is: "
errorMessage: .asciiz "Input out of bounds\n"
Here are some outputs in the screenshots:
Comment in case of any doubts.
Get Answers For Free
Most questions answered within 1 hours.