본문 바로가기
Programming/SPARC

[SPARC] A5/암호화

by 생각하는달팽이 2015. 8. 27.

 




 

아... 이번과제도 6시간이라는 오랜시간동안 삽질을 하며 했는데 결과물이 대학원생님꺼랑 달라서..

멘붕온다음 그냥 올립니다...

 

도중에 stream key 값이 1차이 나면서 틀어지기 시작한 녀석이 어마어마하게 달라졌다는 ... 슬픈 소식입니다.. ㅜ

 

이번 과제를 통해서 시프트 연산과 조건 분기문을 숙달할 수 있었습니다.


토나온다....


소스코드는 제가 아직 완성을 못시켰네요.. 아무래도 다음날 LAB 실가서 차이나는 부분 고치고 업로드하도록 하겠습니다.

으차차~ 재미난 어셈블리어

나중에 임베디드할 기회가 생긴다면 써먹어보고 싶네용..

 

다들 파이팅 !!


아래는 제 소스코드입니다.. ( _ _ )


 

.section ".data"

X:.word 0

Y:.word 0

Z:.word 0

S1:.word 0

S2:.word 0

CHECK19MAX:.word 524287 

CHECK22MAX:.word 4294303 

CHECK23MAX:.word 8388607 

CHECKLASTBIT:.word 1

 

    .section ".text"

fmt0:.asciz "input X (0~524287):"

fmt1:.asciz "input Y (0~4294303):"

fmt2:.asciz "input Z (0~8388607):"

fmt3:.asciz "%d"

fmt4:.asciz "result: 0x%08x%08x\n"

fmt5:.asciz "result:"

    .align 4

    .global main,scanf,printf

main:save %sp,-192,%sp !i,o,l register size

reInputX:set fmt0,%o0

     call printf

     nop

     set fmt3,%o0

     set X,%o1

     call scanf

     nop

     set X,%l0

     ld [%l0],%l0

     set CHECK19MAX,%l1

     ld [%l1],%l1

     cmp %l0,%l1

     bg reInputX

     nop

reInputY:set fmt1,%o0

     call printf

     nop

     set fmt3,%o0

     set Y,%o1

     call scanf

     nop

     set Y,%l0

     ld [%l0],%l0

     set CHECK22MAX,%l1

     ld [%l1],%l1

     cmp %l0,%l1

     bg reInputY

     nop

reInputZ:set fmt2,%o0

     call printf

     nop

     set fmt3,%o0

     set Z,%o1

     call scanf

     nop

     set Z,%l0

     ld [%l0],%l0

     set CHECK23MAX,%l1

     ld [%l1],%l1

     cmp %l0,%l1

     bg reInputZ

     nop

     clr %l5 !i=0

loop:set X,%l0

     ld [%l0],%o0 ! %o0 = X

     set Y,%l1

     ld [%l1],%o1 ! %o1 = Y

     set Z,%l2

     ld [%l2],%o2 ! %o2 = Z

     call maj

     nop

     mov %o0,%o3 ! %o3==m m 구함.

     set X,%l0

     ld [%l0],%o0 ! %o0 = X

     set Y,%l1

     ld [%l1],%o1 ! %o1 = Y

     set Z,%l2

     ld [%l2],%o2 ! %o2 = Z

     call shifting

     nop

     mov %o0,%l0

     mov %o1,%l1

     mov %o2,%l2

loopT1:set X,%i0

     set Y,%i1

     set Z,%i2

     and %l0,%o0,%l0

     and %l1,%o1,%l1

     and %l2,%o2,%l2

     st %l0,[%i0] ! X 쉬프팅 저장됨

     st %l1,[%i1] ! Y 쉬프팅 저장됨

     st %l2,[%i2] ! Z 쉬프팅 저장됨

     srl %l0,18,%l0

     srl %l1,21,%l1

     srl %l2,22,%l2

     and %l0,1,%l0 ! X 18번째 

     and %l1,1,%l1 ! Y 21 번째

     and %l2,1,%l2 ! Z 22 번째

     xor %l0,%l1,%l1 !

     xor %l1,%l2,%l6 ! %l0 == s = (x18 xor y21) xor z22

getSBit:cmp %l5,32

     bl setS1

     nop

     set S2,%i0

     ld [%i0],%o0

     sll %o0,1,%o0

     and %l6,1,%l6

     or %o0,%l6,%o0

     set S2,%l0

     st %o0,[%l0]

plusi:cmp %l5,64

     bl loop ! if i<64 goto loop with i++

     inc %l5 ! i++

     set fmt4,%o0

     set S1,%l0

     set S2,%l1

     ld [%l0],%o1

     ld [%l1],%o2

     call printf

     nop

test:ret

     restore

setS1:set S1,%i0

  ld [%i0],%o0

  sll %o0,1,%o0

  and %l6,1,%l6

  or %o0,%l6,%o0

  set S1,%l0

  st %o0,[%l0]

setT0:ba plusi

      nop

maj:save %sp,-96,%sp !i,l,o register size

mov 1,%l5

    srl %i0,8,%l2

    srl %i1,10,%l3

    srl %i2,10,%l4

    and %l2,1,%l2 !  X 8번째 비트

    and %l3,1,%l3 ! Y 10번째 비트

    and %l4,1,%l4 ! Z 10번째 비트

    add %l2,%l3,%l3 ! SUM last bit of X,Y,Z

    add %l3,%l4,%l2 !  SUM last bit of X,Y,Z

majTest1:cmp %l2,1 ! if Sum > 1 ? returnOne : returnZero

    ble returnZero

    nop

    ba returnOne

    nop

returnZero:clr %i0

    ret

    restore

returnOne:mov 1,%i0

    ret

    restore

shifting:save %sp,-96,%sp !i,l,o register size

         srl %i0,8,%l2

         srl %i1,10,%l3

         srl %i2,10,%l4

         and %l2,1,%l2 ! check last bit of shifted X

         and %l3,1,%l3 ! check last bit of shifted Y

         and %l4,1,%l4 ! check last bit of shifted Z

         cmp %l2,%i3 ! X ? m

         bne stepY

         nop

stepX0: srl %i0,13,%o0

and %o0,1,%o0 ! 13번째 X

srl %i0,16,%o1 ! 

         and %o1,1,%o1 ! 16번째 X

srl %i0,17,%o2 ! 

         and %o2,1,%o2 ! 17번째 X         

         srl %i0,18,%o3 ! 

         and %o3,1,%o3 ! 18번째 X

         xor %o0,%o1,%o1

         xor %o1,%o2,%o2

         xor %o2,%o3,%o3 ! << t

         sll %i0,1,%l2

         or %l2,%o3,%l2

         mov %l2,%i0 ! finish Y shift

stepY:   cmp %l3,%i3 ! Y ? m

         bne stepZ

         nop

         srl %i1,21,%l0

         and %l0,1,%o0

         srl %i1,20,%l0

         and %l0,1,%o1

         xor %o0,%o1,%o0

         sll %i1,1,%l3

         or %l3,%o0,%l3

         mov %l3,%i1 ! finish Y shift

stepZ:   cmp %l4,%i3 ! Z ? m

         bne finShift

         nop

         srl %i2,22,%l0

         and %l0,1,%o0

         srl %i2,21,%l0

         and %l0,1,%o1

         srl %i2,20,%l0

         and %l0,1,%o2

         srl %i2,7,%l0

         and %l0,1,%o3

         xor %o3,%o2,%o3

         xor %o3,%o1,%o3

         xor %o3,%o0,%o3

         sll %i2,1,%l4

         or %l4,%o3,%l4

         mov %l4,%i2 ! finish Z shift

finShift:ret

 

         restore



 

 

반응형

'Programming > SPARC' 카테고리의 다른 글

[SPARC] 비트처리 연산  (2) 2015.08.27
[SPARC] 부동소수점 매개변수 전달  (0) 2015.08.27
[SPARC] 표준 입출력과 파일 입출력  (2) 2015.08.27