COEN311 - Sample MC68000 Programs

PROGRAM 1

    This program will search for the occurrences of a key value in a given series of integers. It scans the array of numbers, checks if the number is equal to the key, increments the counter until reading all the elements.

 

              

org

$4000

 

    

lea

ints,a0

 

 

clr.l

d0

 

 

move.b

size,d0

 

 

subq.b

#1,d0

; loop "size" times with dbra inst. 

 

clr.b

d1

; counter

 

move.b

key,d3

 

 loop

move.b

(a0)+,d2

; read number

 

cmp.b

d3,d2

 

 

bne

skip

 

 

addq.b

#1,d1

 

 skip

dbra

d0,loop

 

 

move.b

d1,r

; store count in memory

 

move.w

#228,d7

 

 

trap

#14

 

 ints

dc.b

1,0,1,1,1,0,0,0,1,0 

 

 size

dc.b

*-ints

 

 key

dc.b

1

 

 r

ds.w

1

 

 

end

 

 


PROGRAM 2

    This program will check if a given number has even parity, then it sets the V bit in CCR accordingly; otherwise it resets it. We rotate the number to the right, the LSB is copied into the X bit of CCR, which is added to the counter of ones. At the end, we check if the total number of 1's is even or odd.

 

              

org

$4000

 

 

move.l

#$1234abcd,d0 ;15 ones in the number
 

moveq.l

 #31,d2 ; loop 32 times with dbra inst.
 

clr.l

d1 ; counter
 

clr.l

d3  
loop

roxr.l

#1,d0  
 

addx.b

d3,d1  
 

dbra

d2,loop  
 

move.l

d1,d5  
 

and.l

#1,d1  
 

 bne      

odd        
even  

ori

#02,ccr ; v=1  
 

bra

finish  
odd

andi.b

#$fd,ccr ; v = 0  
finish    

 move

sr,d4 ;to check ccr at this point
 

move.b

#228,d7  
 

 trap

#14  
 

end

   


PROGRAM 3

This program searches for a given key value in an array of integers. A bit in memory is set to 1 if the number is found. The program scans all the elements in the array, checks if the current number is equal to the provided key value.

 

              

org

$4000

 

 

lea

 ints,a0  
 

clr.l

d0  
 

move.b

size,d0  
 

subq.b

#1,d0  ;loop 10 times with dbra inst.
 

move.b

key,d3  
 

loop    

move.b (a0)+,d2  ;read number
 

cmp.b

d3,d2  
 

beq

found  
 

dbra

d0,loop  
notfnd  

move.b

#0,res  
 

bra

finish  
found   

move.b

#$ff,res  
finish    

move.w

#228,d7  
 

trap

#14  
ints      

dc.b

1,2,3,4,5,22,33,44,55,66,77,88,99,100  
size      

dc.b *-ints

   
key 

dc.b 56

   
res

ds.b 1

   
  end    


PROGRAM 4

   This program implements the bubble sort using subroutine. The address of the array to be sorted and its size are passed using the stack.
 
 

org $4000

   
 

move.w

size,-(sp)      
 

pea

array; -(sp) ; push address of array into stack
 

jsr

bubble  
 

move.b

 #228,d7  
 

trap

#14  
array  

dc.w 

99,88,1,2,3,77,66,6,5,4,8,9  
size      

dc.w  

 *-array  
       
bubble

equ

 *  
 

move.l

4(sp),a0  ; pop address of array
 

clr.l        

d0   
 

move.w

8(sp),d0  ; pop size (in bytes)
 

divs

#2,d0    ; calculate number of items 
loop2  

move.l

a0,a2    
 

move.l

a0,a1        
 

add.l

#2,a2  
 

move.w

d0,d1  
 

subi.w

#1,d1  
loop1  

cmp.w

 (a1)+,(a2)+  
 

bge

 skip      
 

move.w

(a1),d3 ; swap numbers
 

move.w  

-2(a1),(a1)      
 

move.w

d3,-2(a1)  
skip     

dbra

d1,loop1  
 

subi.w

 #1,d0  
 

bgt

 loop2  
 

rts    

   
  end    


PROGRAM 5

    This program counts prime numbers in an array using subroutine. It takes every element in the array, calls a subroutine to check if this element is prime, then adds 1 to the counter. The subroutine takes one argument passed through d0, and returns 1 if it is prime or 0 if it is not. To check for a prime number x, we divide the number on all numbers between 2 and x/2. If the remainder is 0 for one division the number is not prime.

 
 

org

$4000  
 

clr.l

d0  
 

clr.l

d2  
 

lea

 numbers,a0  
loop

move.w

(a0)+,d0  
 

beq

finish  
 

jsr

prime  
 

add.w

d0,d2  
 

bra

loop  
finish    

move.b

#228,d7  
 

trap

#14  
numbers  

dc.w

6,15,4,7,5,101,8,9,105,$11,47,12,0  
       

prime

equ *  
 

movem.l

d1-d7,-(a7)  
 

movem.l

a0-a6,-(a7)      
 

clr.l

d1  
 

move.w

d0,d1  
 

divu

#2,d1     
loop2  

move.l

d0,d2 ; to clear upper part since it is updated in previous loop!!
 

divu

d1,d2       
 

swap

d2  
 

cmpi.w

#0,d2  
 

beq

no  
 

subi.w

#1,d1  
 

cmp.w

#1,d1  
 

bne

loop2  
yes      

move.w  

#1,d0   
 

bra

done  
no       

clr.w

d0  
done    

movem.l

(a7)+,a0-a6  
 

movem.l

(a7)+,d1-d7    
 

rts 

   
  end    


PROGRAM 6

    This program is the same above program that checks if the number is prime using macros

 
prime

macro 

   
 

movem.l

d0-d7,-(a7)  
 

movem.l

a0-a6,-(a7)    
 

clr.l

d1  
 

move.w

\1,d1  
 

divu

#2,d1  
loop2\@

move.l

\1,d2  
 

divu

d1,d2  
 

swap

d2  
 

cmpi.w

#0,d2  
 

beq

no\@  
 

subi.w

#1,d1  
 

cmp.w

#1,d1  
 

bne

loop2\@  
yes\@

move.w  

#1,\2   
 

bra

done\@  
no\@   

move.w

#0,\2  
done\@

movem.l

(a7)+,a0-a6  
 

movem.l    

(a7)+,d0-d7    
 

endm

   
       
 

org

$4000  
 

clr.l

d0  
 

clr.l

d1  
 

clr.l

d2   
 

subq.w

#1,d1  
 

lea

numbers,a0  
loop    

move.w

(a0)+,d0  
 

beq

finish  
 

prime

d0,r ; number and result
 

add.w

 r,d2  
 

bra

loop  
finish    

move.b

 #228,d7  
 

trap

#14  
numbers

dc.w 

6,15,4,7,5,101,8,9,105,$11,$d,47,12,0  
r

ds.w

1  
  end    


PROGRAM 7

 This program implements a string comparison function, and returns:
            -1 if str1 > str2
             0 if str1 = str2
             1 if str1 < str2
This program checks the order of words in a dictionary.

 
 

org

$4000  
 

move.l

#string1,-(sp)  
 

move.l

#string2,-(sp)  
 

move.l

#result,-(sp)  
 

bsr

str_cmp  
 

move.w

#228,d7  
 

trap

#14  
string1 

dc.b

'f','i','r','s','t',0  
string2 

dc.b

's','e','c','o','n','d',0  
result   

ds.w  

1  
       
str_cmp

 equ

*  
 

movem.l

d0-d7,-(sp)  
 

movem.l

a0-a6,-(sp)  
 

move.l

72(sp),a0 ; retrieve string1
 

move.l

68(sp),a1  ;retrieve string2
 

            move.l

64(sp),a2 ; retrieve string2
oop

      cmp.b

 (a1)+,(a0)+  
 

bgt

greater ; string1 > string2
 

blt

 less  ; string1 < string2
 

cmp.b

#0,-1(a0) ; otherwise they are equal
 

beq

equal   ; then we check for null
 

bra

loop  
greater

move.w

#-1,64(sp  
 

bra

done  
less

move.w

#1,(a2)  
 

bra

done  
equal

move.w

#0,(a2)  
done movem.l (sp)+,a0-a6  
  movem.l (sp)+,d0-d7  
  rts    
  end    


PROGRAM 8

This program implements a string concatenation function as follows:
        STRING1 = STRING1 + STRING2
The program adds the second string to the first one, then appends NULL at the end.

str_cat 

macro

    ; takes two absolute long addresses
 

movem.l

d0-d7,-(sp)  
 

            movem.l

a0-a6,-(sp)  
 

move.l #\1,a0

   
 

move.l

#\2,a1  
loop1\@

             cmp.b

#0,(a0) ; move a0 to point to string1
 

                       beq

loop2  
 

                        adda.l

#1,a0  
 

bra

loop1\@  
loop2\@ 

          move.b

(a1)+,(a0)+      ; copy char
 

            cmp.b

 #0,-1(a0)  ; check for null
 

bne

 loop2\@  
done\@  

movem.l

(sp)+,a0-a6  
 

                        movem.l

(sp)+,d0-d7  
 

                        endm

   
       
 

org

$4000  
 

str_cat

string1,string2  
 

str_cat

string3,string4  
 

move.w

#228,d7  
 

trap

#14  
string1           

dc.b

'h','e','l','l','o',0  
space1           

ds.b

24  
string2            

dc.b

 'w','o','r','l','d',0  
string3            

dc.b

'c','o','n','c','o','r','d','i','a',0  
space2

            ds.b

 24  
string4            

dc.b

'u','n','i','v','e','r','s','i','t','y',0  
 

            end