home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.ee.pdx.edu
/
2014.02.ftp.ee.pdx.edu.tar
/
ftp.ee.pdx.edu
/
pub
/
users
/
Harry
/
compilers
/
p11
/
tst
/
primes.out.bak
< prev
next >
Wrap
Text File
|
2006-03-05
|
10KB
|
568 lines
!
! PCAT Compiler Version 1.0
!
.global .div
.global .rem
!
! Standard data fields
!
.data
.align 8
temp: .double 0
.text
strNL: .asciz "\n"
strInt: .asciz "%d"
strFlt: .asciz "%g"
strTrue: .asciz "TRUE"
strFalse: .asciz "FALSE"
message1: .asciz "Execution Error: Allocation failed!\n"
message2: .asciz "Execution Error: Pointer is NIL!\n"
message3: .asciz "Execution Error: Read statement failed!\n"
message4: .asciz "Execution Error: Array index is out of bounds!\n"
message5: .asciz "Execution Error: Count is not positive in array constructor!\n"
.align 8
!
! runtimeError1-5
!
! Branch to one of these labels to print an error message and abort.
!
runtimeError1:
set message1,%o0
call printf
nop
call exit
mov 1,%o0
runtimeError2:
set message2,%o0
call printf
nop
call exit
mov 1,%o0
runtimeError3:
set message3,%o0
call printf
nop
call exit
mov 1,%o0
runtimeError4:
set message4,%o0
call printf
nop
call exit
mov 1,%o0
runtimeError5:
set message5,%o0
call printf
nop
call exit
mov 1,%o0
! writeFlt
!
! This routine is passed a single precision floating number in %f0.
! It prints it by calling printf. It uses registers %f0, %f1.
!
writeFlt:
save %sp,-128,%sp
fstod %f0,%f0
set temp,%l0
std %f0,[%l0]
ldd [%l0],%o0
mov %o1,%o2
mov %o0,%o1
set strFlt,%o0
call printf
nop
ret
restore
! writeBool
!
! This routine is passed an integer in %i0/o0. It prints "FALSE" if this
! integer is 0 and "TRUE" otherwise.
!
writeBool:
save %sp,-128,%sp
cmp %i0,%g0
be printFalse
nop
set strTrue,%o0
ba printEnd
nop
printFalse:
set strFalse,%o0
printEnd:
call printf
nop
ret
restore
!
! Additional Fields
!
.data
display0: .word 0
.text
str2: .asciz "N must be >= 1. Try again..."
str1: .asciz "Printing the primes from 1 to N; Please enter N..."
.align 8
! MAIN...
! mainEntry
.global main
main: save %sp,-208,%sp
set display0,%o0
st %fp,[%o0]
! VAR INITIALIZATION...
! N := 0
set 0,%o0
st %o0,[%fp+-4]
! VAR INITIALIZATION...
! a := 0
set 0,%o0
st %o0,[%fp+-8]
! VAR INITIALIZATION...
! i := 0
set 0,%o0
st %o0,[%fp+-12]
! VAR INITIALIZATION...
! j := i
ld [%fp+-12],%o0
st %o0,[%fp+-16]
! WRITE STMT...
! writeString str1
sethi %hi(str1),%o0
call printf
or %o0,%lo(str1),%o0
! writeNewline
sethi %hi(strNL),%o0
call printf
or %o0,%lo(strNL),%o0
! LOOP...
! Label_1:
Label_1:
! READ STMT...
! t1 := &N
add %fp,-4,%o0
st %o0,[%fp+-20]
! readInt t1
ld [%fp+-20],%o1
sethi %hi(strInt),%o0
call scanf
or %o0,%lo(strInt),%o0
cmp %o0,0
be runtimeError3
nop
! IF...
! if N >= 1 goto Label_3 (integer)
ld [%fp+-4],%o0
set 1,%o1
cmp %o0,%o1
bge Label_3
nop
! goto Label_4
ba Label_4
nop
! Label_3:
Label_3:
! THEN...
! EXIT...
! goto Label_2
ba Label_2
nop
! END IF...
! Label_4:
Label_4:
! WRITE STMT...
! writeString str2
sethi %hi(str2),%o0
call printf
or %o0,%lo(str2),%o0
! writeNewline
sethi %hi(strNL),%o0
call printf
or %o0,%lo(strNL),%o0
! END LOOP...
! goto Label_1
ba Label_1
nop
! Label_2:
Label_2:
! ASSIGNMENT STMT...
! t2 := 0
set 0,%o0
st %o0,[%fp+-24]
! t3 := N + 1 (integer)
ld [%fp+-4],%o0
set 1,%o1
add %o0,%o1,%o1
st %o1,[%fp+-28]
! if t3 <= 0 goto runtimeError5 (integer)
ld [%fp+-28],%o0
set 0,%o1
cmp %o0,%o1
ble runtimeError5
nop
! t2 := t2 + t3 (integer)
ld [%fp+-24],%o0
ld [%fp+-28],%o1
add %o0,%o1,%o1
st %o1,[%fp+-24]
! t4 := t2 * 4 (integer)
ld [%fp+-24],%o0
set 4,%o1
smul %o0,%o1,%o1
st %o1,[%fp+-32]
! t4 := t4 + 4 (integer)
ld [%fp+-32],%o0
set 4,%o1
add %o0,%o1,%o1
st %o1,[%fp+-32]
! t4 := alloc (t4)
set 1,%o0
ld [%fp+-32],%o1
call calloc
nop
st %o0,[%fp+-32]
! if t4 = 0 goto runtimeError1 (integer)
ld [%fp+-32],%o0
set 0,%o1
cmp %o0,%o1
be runtimeError1
nop
! t5 := t4
ld [%fp+-32],%o0
st %o0,[%fp+-36]
! *t4 := t2
ld [%fp+-24],%o0
ld [%fp+-32],%o1
st %o0,[%o1]
! t6 := t3
ld [%fp+-28],%o0
st %o0,[%fp+-40]
! Label_5:
Label_5:
! t4 := t4 + 4 (integer)
ld [%fp+-32],%o0
set 4,%o1
add %o0,%o1,%o1
st %o1,[%fp+-32]
! *t4 := 1
set 1,%o0
ld [%fp+-32],%o1
st %o0,[%o1]
! t6 := t6 - 1 (integer)
ld [%fp+-40],%o0
set 1,%o1
sub %o0,%o1,%o1
st %o1,[%fp+-40]
! if t6 > 0 goto Label_5 (integer)
ld [%fp+-40],%o0
set 0,%o1
cmp %o0,%o1
bg Label_5
nop
! a := t5
ld [%fp+-36],%o0
st %o0,[%fp+-8]
! ASSIGNMENT STMT...
! if a = 0 goto runtimeError2 (integer)
ld [%fp+-8],%o0
set 0,%o1
cmp %o0,%o1
be runtimeError2
nop
! if 1 < 0 goto runtimeError4 (integer)
set 1,%o0
set 0,%o1
cmp %o0,%o1
bl runtimeError4
nop
! t7 := *a
ld [%fp+-8],%o0
ld [%o0],%o0
st %o0,[%fp+-44]
! if 1 >= t7 goto runtimeError4 (integer)
set 1,%o0
ld [%fp+-44],%o1
cmp %o0,%o1
bge runtimeError4
nop
! t7 := 1 * 4 (integer)
set 1,%o0
set 4,%o1
smul %o0,%o1,%o1
st %o1,[%fp+-44]
! t7 := t7 + 4 (integer)
ld [%fp+-44],%o0
set 4,%o1
add %o0,%o1,%o1
st %o1,[%fp+-44]
! t7 := a + t7 (integer)
ld [%fp+-8],%o0
ld [%fp+-44],%o1
add %o0,%o1,%o1
st %o1,[%fp+-44]
! *t7 := 0
set 0,%o0
ld [%fp+-44],%o1
st %o0,[%o1]
! FOR...
! t11 := &i
add %fp,-12,%o0
st %o0,[%fp+-60]
! t8 := 2
set 2,%o0
st %o0,[%fp+-48]
! t12 := N DIV 2 (integer)
ld [%fp+-4],%o0
set 2,%o1
call .div
nop
st %o0,[%fp+-64]
! t9 := t12
ld [%fp+-64],%o0
st %o0,[%fp+-52]
! t10 := 1
set 1,%o0
st %o0,[%fp+-56]
! *t11 := t8
ld [%fp+-48],%o0
ld [%fp+-60],%o1
st %o0,[%o1]
! Label_6:
Label_6:
! if t8 > t9 goto Label_7 (integer)
ld [%fp+-48],%o0
ld [%fp+-52],%o1
cmp %o0,%o1
bg Label_7
nop
! FOR...
! t16 := &j
add %fp,-16,%o0
st %o0,[%fp+-80]
! t13 := 2
set 2,%o0
st %o0,[%fp+-68]
! t17 := N DIV i (integer)
ld [%fp+-4],%o0
ld [%fp+-12],%o1
call .div
nop
st %o0,[%fp+-84]
! t14 := t17
ld [%fp+-84],%o0
st %o0,[%fp+-72]
! t15 := 1
set 1,%o0
st %o0,[%fp+-76]
! *t16 := t13
ld [%fp+-68],%o0
ld [%fp+-80],%o1
st %o0,[%o1]
! Label_8:
Label_8:
! if t13 > t14 goto Label_9 (integer)
ld [%fp+-68],%o0
ld [%fp+-72],%o1
cmp %o0,%o1
bg Label_9
nop
! ASSIGNMENT STMT...
! if a = 0 goto runtimeError2 (integer)
ld [%fp+-8],%o0
set 0,%o1
cmp %o0,%o1
be runtimeError2
nop
! t18 := i * j (integer)
ld [%fp+-12],%o0
ld [%fp+-16],%o1
smul %o0,%o1,%o1
st %o1,[%fp+-88]
! if t18 < 0 goto runtimeError4 (integer)
ld [%fp+-88],%o0
set 0,%o1
cmp %o0,%o1
bl runtimeError4
nop
! t19 := *a
ld [%fp+-8],%o0
ld [%o0],%o0
st %o0,[%fp+-92]
! if t18 >= t19 goto runtimeError4 (integer)
ld [%fp+-88],%o0
ld [%fp+-92],%o1
cmp %o0,%o1
bge runtimeError4
nop
! t19 := t18 * 4 (integer)
ld [%fp+-88],%o0
set 4,%o1
smul %o0,%o1,%o1
st %o1,[%fp+-92]
! t19 := t19 + 4 (integer)
ld [%fp+-92],%o0
set 4,%o1
add %o0,%o1,%o1
st %o1,[%fp+-92]
! t19 := a + t19 (integer)
ld [%fp+-8],%o0
ld [%fp+-92],%o1
add %o0,%o1,%o1
st %o1,[%fp+-92]
! *t19 := 0
set 0,%o0
ld [%fp+-92],%o1
st %o0,[%o1]
! END FOR...
! t13 := *t16
ld [%fp+-80],%o0
ld [%o0],%o0
st %o0,[%fp+-68]
! t13 := t13 + t15 (integer)
ld [%fp+-68],%o0
ld [%fp+-76],%o1
add %o0,%o1,%o1
st %o1,[%fp+-68]
! *t16 := t13
ld [%fp+-68],%o0
ld [%fp+-80],%o1
st %o0,[%o1]
! goto Label_8
ba Label_8
nop
! Label_9:
Label_9:
! END FOR...
! t8 := *t11
ld [%fp+-60],%o0
ld [%o0],%o0
st %o0,[%fp+-48]
! t8 := t8 + t10 (integer)
ld [%fp+-48],%o0
ld [%fp+-56],%o1
add %o0,%o1,%o1
st %o1,[%fp+-48]
! *t11 := t8
ld [%fp+-48],%o0
ld [%fp+-60],%o1
st %o0,[%o1]
! goto Label_6
ba Label_6
nop
! Label_7:
Label_7:
! FOR...
! t23 := &i
add %fp,-12,%o0
st %o0,[%fp+-108]
! t20 := 1
set 1,%o0
st %o0,[%fp+-96]
! t21 := N
ld [%fp+-4],%o0
st %o0,[%fp+-100]
! t22 := 1
set 1,%o0
st %o0,[%fp+-104]
! *t23 := t20
ld [%fp+-96],%o0
ld [%fp+-108],%o1
st %o0,[%o1]
! Label_10:
Label_10:
! if t20 > t21 goto Label_11 (integer)
ld [%fp+-96],%o0
ld [%fp+-100],%o1
cmp %o0,%o1
bg Label_11
nop
! IF...
! if a = 0 goto runtimeError2 (integer)
ld [%fp+-8],%o0
set 0,%o1
cmp %o0,%o1
be runtimeError2
nop
! if i < 0 goto runtimeError4 (integer)
ld [%fp+-12],%o0
set 0,%o1
cmp %o0,%o1
bl runtimeError4
nop
! t24 := *a
ld [%fp+-8],%o0
ld [%o0],%o0
st %o0,[%fp+-112]
! if i >= t24 goto runtimeError4 (integer)
ld [%fp+-12],%o0
ld [%fp+-112],%o1
cmp %o0,%o1
bge runtimeError4
nop
! t24 := i * 4 (integer)
ld [%fp+-12],%o0
set 4,%o1
smul %o0,%o1,%o1
st %o1,[%fp+-112]
! t24 := t24 + 4 (integer)
ld [%fp+-112],%o0
set 4,%o1
add %o0,%o1,%o1
st %o1,[%fp+-112]
! t24 := a + t24 (integer)
ld [%fp+-8],%o0
ld [%fp+-112],%o1
add %o0,%o1,%o1
st %o1,[%fp+-112]
! t25 := *t24
ld [%fp+-112],%o0
ld [%o0],%o0
st %o0,[%fp+-116]
! if t25 = 0 goto Label_13 (integer)
ld [%fp+-116],%o0
set 0,%o1
cmp %o0,%o1
be Label_13
nop
! goto Label_12
ba Label_12
nop
! Label_12:
Label_12:
! THEN...
! WRITE STMT...
! writeInt i
ld [%fp+-12],%o1
sethi %hi(strInt),%o0
call printf
or %o0,%lo(strInt),%o0
! writeNewline
sethi %hi(strNL),%o0
call printf
or %o0,%lo(strNL),%o0
! END IF...
! Label_13:
Label_13:
! END FOR...
! t20 := *t23
ld [%fp+-108],%o0
ld [%o0],%o0
st %o0,[%fp+-96]
! t20 := t20 + t22 (integer)
ld [%fp+-96],%o0
ld [%fp+-104],%o1
add %o0,%o1,%o1
st %o1,[%fp+-96]
! *t23 := t20
ld [%fp+-96],%o0
ld [%fp+-108],%o1
st %o0,[%o1]
! goto Label_10
ba Label_10
nop
! Label_11:
Label_11:
! MAIN EXIT...
! mainExit
ret
restore