Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help ASAP! I need this in Assembly Language code/Easy68K please. Write a

ID: 3824146 • Letter: P

Question

Please help ASAP! I need this in Assembly Language code/Easy68K please.

Write a program called SortedPrimes to

a. Find out the prime numbers from the following list (15, 16, 17, 23,
2, 32, 3, 31, 13, 19, 12, 9) and push/store them in a stack and then
print. (15 points)

b. Sort the list of prime numbers that you found from previous
problem in descending order and store/push them in a stack and
then print. (15 points)

Output should look like below on console:
Prime Numbers: 17 23 2 3 31 13 19
Sorted in Descending Order: 31 23 19 17 13 3 2

Explanation / Answer

07

org 100h; set location counter to 100h

11

DataStart:

12

   max dw 31

13

   space db " ", 0

14

15

CodeStart:

16

   mov bx, 1

17

    

18

   LoopStart:

19

    

20

      ; must be a prime

21

       mov ax, bx

22

       call print_num

23

        

24

      ; print a space

25

       mov si, offset space

26

       call print_string

27

        

28

       add bx, 1

29

       cmp bx, max

30

31

   jle LoopStart

32

    

33

   ret

34

35

   IsPrime PROC

36

      ; uses a loop to determine if number in bx is prime

37

      ; upon return if bx not prime dx will be 0, otherwise dx > 0

38

39

      ; we only have to test divisors from 2 to bx/2

40

        

41

      ; prepare to divide dx:ax / 2

42

       mov ax, bx       

43

       mov dx, 0

44

       mov cx, 2

45

       div cx

46

        

47

      ; move result into si for loop

48

       mov si, ax

49

        

50

      ; assume the value is prime

51

       mov dx, 1

52

        

53

      ; start loop at 2

54

       mov cx, 2

55

        

56

       PrimeLoop:

57

        

58

          ; compare loop count(in cx) and max loop value (in si)

59

           cmp cx, si

60

            

61

          ; jump out of loop if count(cx) > si

62

           ja StopLabel

63

        

64

          ; divide test value (in bx) by loop count (in cx)

65

           mov ax, bx

66

           mov dx, 0           

67

           div cx

68

            

69

          ; check remainder (in dx), if zero then we found a divisor

70

          ; and the number cannot be prime

71

           cmp dx, 0

72

            

73

          ; if dx = 0 then we found a divisor and can stop looking

74

           je StopLabel

75

            

76

          ; increment count

77

           add cx, 1

78

        

79

       jmp PrimeLoop

80

        

81

       StopLabel:

82

        

83

       ret

84

   IsPrime ENDP

85

    

86

DEFINE_PRINT_STRING

87

DEFINE_SCAN_NUM

88

DEFINE_PRINT_NUM

89

DEFINE_PRINT_NUM_UNS

01

; prime.asm

02

; sample program to demonstrate procedures

03

; calulates and prints prime numbers from 1 to 20

04

05

include 'emu8086.inc'

06

07

org 100h; set location counter to 100h

08

09

jmp CodeStart

10

11

DataStart:

12

   max dw 20

13

   space db " ", 0

14

15

CodeStart:

16

   mov bx, 1

17

    

18

   LoopStart:

19

    

20

      ; must be a prime

21

       mov ax, bx

22

       call print_num

23

        

24

      ; print a space

25

       mov si, offset space

26

       call print_string

27

        

28

       add bx, 1

29

       cmp bx, max

30

31

   jle LoopStart

32

    

33

   ret

34

35

   IsPrime PROC

36

      ; uses a loop to determine if number in bx is prime

37

      ; upon return if bx not prime dx will be 0, otherwise dx > 0

38

39

      ; we only have to test divisors from 2 to bx/2

40

        

41

      ; prepare to divide dx:ax / 2

42

       mov ax, bx       

43

       mov dx, 0

44

       mov cx, 2

45

       div cx

46

        

47

      ; move result into si for loop

48

       mov si, ax

49

        

50

      ; assume the value is prime

51

       mov dx, 1

52

        

53

      ; start loop at 2

54

       mov cx, 2

55

        

56

       PrimeLoop:

57

        

58

          ; compare loop count(in cx) and max loop value (in si)

59

           cmp cx, si

60

            

61

          ; jump out of loop if count(cx) > si

62

           ja StopLabel

63

        

64

          ; divide test value (in bx) by loop count (in cx)

65

           mov ax, bx

66

           mov dx, 0           

67

           div cx

68

            

69

          ; check remainder (in dx), if zero then we found a divisor

70

          ; and the number cannot be prime

71

           cmp dx, 0

72

            

73

          ; if dx = 0 then we found a divisor and can stop looking

74

           je StopLabel

75

            

76

          ; increment count

77

           add cx, 1

78

        

79

       jmp PrimeLoop

80

        

81

       StopLabel:

82

        

83

       ret

84

   IsPrime ENDP

85

    

86

DEFINE_PRINT_STRING

87

DEFINE_SCAN_NUM

88

DEFINE_PRINT_NUM

89

DEFINE_PRINT_NUM_UNS