Jump to content

File talk:Motorola 6800 Assembly Language.png

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Untitled

[edit]

Hi I'm trying to print prime numbers from number1 and number2 Program gives output always from 2 till number2 istead of number1 If the first number is greater than the second it still shows 2 to number 2 instead of number 1 to number 2

.data

  1. Displays the menu options

menu: .ascii "\n" .ascii "1, Enter Number 1\n" .ascii "2, Enter Number 2\n" .ascii "3, Display prime numbers between num1 and num2\n" .ascii "4, Quit\n" .ascii "\n" .ascii "Please enter menu option : " .asciiz ""

  1. Strings to display before after chosing appropriate options make the input and output look meaningful

str.first: .asciiz "\nEnter first number :\t" str.second: .asciiz "\n Enter Second number :\t" str.combine: .asciiz " and " str.space: .asciiz " \t " str.newline: .asciiz " \n " str.prime: .asciiz " \n The prime numbers of : "


  1. initialization of registers these used for prime numbers

i: .word 2

p: .word 0

k: .word 2

c: .word 1


d: .word 2


.globl main

.text

  1. setting default values to stroring numbers if user wants to choose direct option 3 or higher

addi $s1,$zero, 2 # default Value for num1 addi $s2,$zero, 8 # default Value for num2

  1. Print Menu using service num 4

printMenu: # Label so we can jump/branch back here

la $a0, menu #set $a0 to label menu 's address addi $v0, $zero, 4 syscall

  1. Read user option using service 5 [chosen menu num goes into $v0]

addi $v0, $zero, 5 syscall

  1. move user's choice into $s3

add $s3,$zero, $v0


  1. process menu

beq $s3,1,opt1 beq $s3,2 opt2 beq $s3,3,opt3 beq $s3,4,quit

  1. user selected an invalid option so print menu again

j printMenu

opt1:

  1. do this if user selected 1
  2. User input for the first number

addi $v0, $zero, 4 la $a0, str.first #set labels address into $a0 syscall #Displays Enter first number from $a0

addi $v0, $zero, 5 #Reads input from keyboard and stores in $v0 syscall addi $s1, $v0,0 # set FIRST NUMBER(s1) to user enter input(v0)

j printMenu # jump back and print menu again

opt2:

  1. do this if user selected 2
  2. User input for the second number

addi $v0, $zero, 4 la $a0, str.second #set labels address into $a0 syscall #Displays Enter Second number from $a0

addi $v0, $zero, 5 #Reads input from keyboard and stores in $v0 syscall addi $s2, $v0,0 # set SECOND NUMBER(s2) to user enter input(v0)

j printMenu


  1. DO THIS IF OPTION 3 IS SELECTED
  2. Prime numbers will be displayed between num1 and num2

opt3: main:

li $v0, 4 # display message to user la $a0, str.prime #load address of the label in $a0 syscall #Display the string 'The prime numbers of : '

li $v0,1 move $a0,$s1 #Entered first number moved to $a0 syscall #Display the first number

addi $v0, $zero, 4 la $a0, str.combine #load address of the label in $a0 syscall #'and' will be displayed in between first and the second number

li $v0,1 move $a0,$s2 #Entered second number moved to $a0 from $s2 syscall #Display the second number

addi $v0, $zero, 4 la $a0, str.newline #load address of the label in $a0 syscall

  1. swapped values

move $t0,$s1 #move contents of $s1 to $t0 move $t7,$s2 #move contents of $s2 to $t7

li $v0, 4 la $a0, str.space #load address of the label in $a0 syscall # call operating sys to leave gap after printing number

  1. Load variables into registers

lw $t1, i # $t1 = i lw $t2, k # $t2 = k lw $t3, p # $t3 = p lw $t5, c # $t5 = c lw $t6, d # $t6 = d

blt $t7,$t1 L2 #Branch to statement at Label L2 if $t7 is less than $t1

li $v0, 1 # print integer function call 1 move $a0, $t1 #move contents of $t1 is moved to $a0 syscall # integer to print call operating sys

li $v0, 4 la $a0, str.space #load address of the label in $a0 syscall # call operating sys to print space or gap

  1. Outer loop

L2: move $t3, $zero

   # Inner loop

L1: remu $t4, $t1, $t2 # remainder set to $t4(remainder of $t1 divided by $t3) bne $t4, $zero, I move $t3, $t5 I: add $t2, $t2, $t5 # increment k blt $t2, $t1 L1

bne $t3, $zero, P

li $v0, 1 # print integer function call 1 move $a0, $t1 # move contents of $t1 to $a0 syscall #prints the integer


li $v0, 4 # print string function call 4 la $a0, str.space #load address of the label in $a0 syscall # call operating sys to print gap or space


P: add $t1, $t1, $t5 # increment i

move $t2, $t6 #move contents $t6 to $t2

bgt $t1, $t7, E #branch to statements at label if $t1 is greater than $t7 j L2


E: j printMenu # jump back and print menu again

  1. DO THIS IF OPTION 12 IS SELECTED

opt4: quit: — Preceding unsigned comment added by 86.15.114.95 (talkcontribs) 11:55, 30 December 2014‎