[SPARC] Bitwise Operations

Problem Definition


Bitwise Operations

We aim to implement operations for a set that stores numbers from 0 to 63. The sets are defined in the static area with the names set1, set2, and set3. Each bit of set1, set2, and set3 represents 63, 62, ... , 2, 1, 0, and the bit is set to 1 if the corresponding number belongs to the set, otherwise it is set to 0. Initially, you just need to define the sets you want to test.

  •  member(j, set): Returns 1 if j is an element of set.

  •  union: Union operation. set3 = set1 ∪ set2

  •  intersection: Intersection operation. set3 = set1 ∩ set2

  •  subtract: Set difference operation. set3 = set1 - set2

  •  add(j, set): Adds element j to the set set.

  •  mapinc(set, d): A set where d is added to each element of set. For example, set({1, 13, 17}, 1) = {2, 14, 18}.

    Submission

  •  Submit a file containing only the definitions of the above 3 functions. Do not submit the file containing main and the definitions of sets set1 and set2. Use them separately for your own testing and execution.


Design

1. Sets are defined in the static area as set1, set2, and set3.
2. Each bit of set1, set2, and set3 represents 63, 62, 61, ..., 2, 1, 0.
3. If the corresponding number is in the set, the bit is set to 1; otherwise, it is 0.
4. Define the initial sets to be tested.

< Mediocre source code.. >

1. member(j, set) function > Result 0 or 1

*To run the source code, please uncomment the bold comments </span> . </span> </span> </div>

!.section ".data"

!set1:.word 1 , 4 , 5 , 16

!set2:.word 1 , 4 , 7 , 23

!

!.section ".text"

!!local variables

!n = -4


! index i in $l0

! max in $l1

! .global main

!main : save %sp, -96 ,%sp

! set set1, %l2

! st %l2,[%sp + 16 ]

! set set2, %l3

! st %l3,[%sp + 32 ]

! mov 16 ,%o0

! call member

! mov %l2,%o1

!test : ret

! restore


!N= 4 ! ( if N = 4 , it 's only works in {a,b,c,d} just 4 objects)

.global member

member : save %sp, -96 ,%sp

mov 1 ,%l0 ! move 1 to l0

sll %l0,N,%l5 ! 2 ^N to l5 (this time 2 ^ 4 = 16 )

add %fp,%l5,%o3 ! address %fp +16

ld [%o3],%o0 ! right value of [%fp +16 ]

cmp %i0,%o0 ! x == [%fp +16 ] ?

be,a isexist ! if yes goto isexist return

ba loop ! or not goto loop

indexup : inc %l0 ! increase index ++

loop: sll %l0, 2 ,%l2 ! address l2 = i* 4

add %o0,%l2,%o1 ! [%fp +16 +i* 4 ]

ld [%o1],%l1 ! load [%fp +16 +i* 4 ]

cmp %i0,%l1 ! x== [%fp +16 +i* 4 ]

be isexist ! if equal then goto isexist

cmp %l0,N ! %l0 < N

bl,a indexup ! if true goto indexup

nop

nonexist: mov 0 ,%i0 ! return value 0 - not exist

ret

restore

isexist: mov 1 ,%i0 ! return value 1 - it 's exist

ret

restore



2. add(j, set) function > { 1,3,4, j }

*To run the source code, please uncomment the bold comments </span> . </span> </span> </div>

!.section ".data"

!set1:.word 1 , 4 , 5 , 16

!

!.section ".text"

!local variables

!n = -4

!

! index i in $l0

! max in $l1

!

! .global main

!main : save %sp, -96 ,%sp

! set set1, %l2

! mov 44 ,%o1

! call add

! mov %l2,%o0

!test : ret

! restore

!N= 4 ! ( if N = 4 , it 's only works in {a,b,c,d} just 4 objects)

.global add

add : save %sp, -96 ,%sp

mov 1 ,%l0

mov %i0,%o0 ;

sll %l0,N,%l5 ! 2 ^N to l5 (this time 2 ^ 4 = 16 )

add %i0,%l5,%i0

st %i1,[%i0]

addend: ret

restore

</div>

3. mapinc(set, d) function > {1,3,4}, d= 1 > {2,4,5}

*To run the source code, please uncomment the bold comments </span> . </span> </span> </div>

!.section ".data"

!set1:.word 1 , 4 , 5 , 16

!set2:.word 1 , 4 , 7 , 23

!

!.section ".text"

!local variables

!n = -4

!

! index i in $l0

! max in $l1

!

! .global main

!main : save %sp, -96 ,%sp

! set set1, %l2

! st %l2,[%sp + 16 ]

! mov 1 ,%o1

! call mapinc

! mov %l2,%o0

!test : ret

! restore

!

!N= 4 ! ( if N = 4 , it 's only works in {a,b,c,d} just 4 objects)

.global mapinc

mapinc : save %sp, -96 ,%sp

mov 1 ,%l0

sll %l0,N,%l5 ! 2 ^N to l5 (this time 2 ^ 4 = 16 )

add %fp,%l5,%o3 ! address %fp +16

ld [%o3],%o0 ! right value of [%fp +16 ]

ba loop ! or not goto loop

clr %l0

indexup : inc %l0 ! increase index ++

loop: sll %l0, 2 ,%l2 ! address l2 = i* 4

add %o0,%l2,%o1 ! [%fp +16 +i* 4 ]

ld [%o1],%l1 ! load [%fp +16 +i* 4 ]

add %l1,%i1,%l1 ! add %l1 = %l1 + %i1

st %l1,[%o1] ! store %l1 to [%fp +16 +i* 4 ]

cmp %l0,N ! compare %l0 < N

bl,a indexup ! if true goto indexup

nop

ret

restore




The mediocre source codes are all finished...
What I felt while doing this assignment was that if you use bit processing when accessing array addresses, you don't need to calculate addresses using a separate multiplication. It's very convenient.
Of course, the source code is not optimized. Because I'm a complete beginner..

It took roughly 6 hours of struggling to write the source code. I debugged it and checked what was in the memory one by one.

Oh, as for the commands used for debugging..

- gcc -g main.c -o main :: Compiles the main.c file so that the global labels can be referenced, and spits out an executable file named 'main'.

- gdb main :: Debugs 'main'.

- p $i0 :: Prints the value of the %i0 register.
- p set1 @docs/superpowers/specs/2026-08-23-mac-release-dmg-design.md :: Checks up to the n-th value of the static variable set1.
ex) -p set1 @docs/superpowers/specs/2026-08-23-mac-release-dmg-design.md >> {1 , 4 , 5 }


My understanding may be very wrong in many places. If you correct me, I will revise it.
Thank you.

</div> </div> </div> </div>
AD