Advertisement
Coriic

Zadania z ASM

Jun 26th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #CZWARTEK 12.30
  2. .data
  3.     occurences:
  4.     .long 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  5. .text
  6. .type check_tab, @function
  7. .global check_tab
  8.  
  9. #tab->n->max
  10. check_tab:
  11.     PUSH %rbx
  12.     XOR %rbx, %rbx
  13.     XOR %rax, %rax
  14.     XOR %r8, %r8
  15.     XOR %r9, %r9
  16.     XOR %rcx, %rcx
  17.     MOV $0, %ecx
  18. clear:
  19.     MOVL $0, occurences(, %ecx, 4)
  20.     INC %ecx
  21.     CMP $64, %ecx
  22.     JNE clear
  23.     XOR %ecx, %ecx
  24.     MOV %rdi, %r9
  25. loop:
  26.     CMP $0, %rsi
  27.     JE afterLoop
  28.     CMPB $0, (%rdi)
  29.     JL afterHashing
  30.     CMPB $63, (%rdi)
  31.     JG afterHashing
  32.     MOV (%rdi), %eax
  33.     MOV occurences(, %eax, 4), %ebx
  34.     INC %ebx
  35.     MOV %ebx, occurences(, %eax, 4)
  36. afterHashing:
  37.     ADD $4, %rdi
  38.     DEC %rsi
  39.     JMP loop
  40.  
  41.  
  42.  
  43. afterLoop:
  44.     MOV $63, %ecx
  45.     XOR %rax, %rax
  46.     XOR %rbx, %rbx
  47.     MOV $0, %r8
  48. loop2:
  49.     XOR %rbx, %rbx
  50.     MOVL occurences(, %ecx, 4), %ebx
  51.     CMP %r8, %rbx
  52.     JG swapMax
  53. afterSwap:
  54.     SHL $1, %rax
  55.     CMP $0, %ebx
  56.     JE afterModifing
  57.     ADD $1, %rax
  58. afterModifing:
  59.     DEC %ecx
  60.     CMP $0,%ecx
  61.     JGE loop2
  62.     MOV %r8,%rbx
  63.     MOVL %ebx, (%rdx)
  64.     MOV %r9, %rdi
  65.     POP %rbx
  66.     RET
  67.  
  68. swapMax:
  69.     MOV %rbx, %r8
  70.     JMP afterSwap
  71.  
  72. #PIĄTEK 8.00
  73. .text
  74. .type fun, @function
  75. .global fun
  76.  
  77. #a - rdi
  78. #b - rsi
  79. #pcs - rdx
  80. #count - rcx
  81. fun:
  82.     PUSH %rbx
  83.     PUSH %rdx
  84.     XOR %rbx, %rbx
  85.     XOR %r9, %r9
  86.     XOR %r10, %r10
  87.     XOR %rax, %rax
  88.     MOV $1, %rbx # (i+1)
  89.     MOV $0, %r9 # how Many 1
  90.     CMP $1, %rsi # which bit
  91.     JE sum
  92.     CMP $2, %rsi
  93.     JE mul
  94.     JMP nothing
  95. loop:
  96.     PUSH %rdi
  97.     AND $1, %rdi
  98.     JMP count
  99. afterCount:
  100.     CMP $0, %rsi
  101.     JE afterOp
  102.     PUSH %rax
  103.     MOV %rdi, %rax
  104.     XOR %rdx, %rdx
  105.     MUL %rbx
  106.     MOV %rax, %r8
  107.     POP %rax
  108.     CMP $1, %rsi
  109.     JE sumOp
  110.     CMP $2, %rsi
  111.     JE mulOp
  112. afterOp:
  113.     POP %rdi
  114.     INC %rbx
  115.     SHR $1, %rdi
  116.     CMP $64, %rbx
  117.     JNE loop
  118. afterLoop:
  119.     POP %rdx
  120.     POP %rbx
  121.     MOVL %r9d, (%rcx)
  122.     MOVL %r10d, (%rdx)
  123.     RET
  124.  
  125. sum:
  126.     MOV $0, %rax #starting value
  127.     JMP loop
  128.  
  129. mul:
  130.     MOV $1, %rax #starting value
  131.     JMP loop
  132.  
  133. nothing:
  134.     MOV $0, %rax
  135.     JMP loop
  136.  
  137. sumOp:
  138.     ADD %r8, %rax #xi in rax
  139.     JMP afterOp
  140.  
  141. mulOp:
  142.     CMP $0, %r8
  143.     JE afterOp
  144.     XOR %rdx, %rdx
  145.     MUL %r8 #xi in rax
  146.     JMP afterOp
  147.  
  148. count:
  149.     CMP $0, %rdi
  150.     JE afterCount
  151.     INC %r9
  152.     MOV %rbx, %r10
  153.     JMP afterCount
  154.  
  155. #PIĄTEK 9.30
  156. .data
  157.     mask:
  158.     .long 0,0,0,0,0,0,0,0,0,0
  159.  
  160. .text
  161. .type encode, %function
  162. .global encode
  163.  
  164. #rdi - buf
  165. #rsi - maska
  166. #rdx - operacja
  167. #rcx - character
  168. encode:
  169.     PUSH %rbx
  170.     PUSH %rdi
  171.     PUSH %rsi
  172.     MOV $0, %rbx
  173. createMask:
  174.     PUSH %rsi
  175.     AND $1, %rsi
  176.     CMP $1, %rsi
  177.     JE changeMask
  178.     MOVL $0, mask(, %rbx, 4)
  179. maskChanged:
  180.     POP %rsi
  181.     SHR $1, %rsi
  182.     INC %rbx
  183.     CMP $10, %rbx
  184.     JNE createMask
  185.    
  186.     XOR %r8, %r8
  187.     MOV %rdi, %rax
  188.     MOV %rdi, %r8
  189. mainLoop:
  190.     CMPB $0, (%rdi)
  191.     JE afterLoop
  192.     CMPB $'0', (%rdi)
  193.     JL noOperation
  194.     CMPB $'9', (%rdi)
  195.     JG noOperation
  196.     CMP $1, %rdx
  197.     JE deleteNumber
  198.     CMP $2, %rdx
  199.     JE transposition
  200.     CMP $3, %rdx
  201.     JE substitute
  202.     JMP noOperation
  203. afterLoop:
  204.     MOV (%rdi), %r9
  205.     MOV %r9, (%r8)
  206.     POP %rsi
  207.     POP %rdi
  208.     POP %rbx
  209.     RET
  210.  
  211. changeMask:
  212.     MOVL $1, mask(, %rbx, 4)
  213.     JMP maskChanged
  214.  
  215. noOperation:
  216.     XOR %r10, %r10
  217.     MOVB (%rdi), %r10b
  218.     MOVB %r10b, (%r8)
  219.     INC %rdi
  220.     INC %r8
  221.     JMP mainLoop
  222.  
  223. deleteNumber:
  224.     XOR %r10, %r10
  225.     MOVB (%rdi), %r10b
  226.     SUBB $'0', %r10b
  227.     CMP $0, mask(,%r10d,4)
  228.     JE noOperation
  229.     INC %rdi
  230.     JMP mainLoop
  231.  
  232. transposition:
  233.     XOR %r10, %r10
  234.     MOVB (%rdi), %r10b
  235.     SUBB $'0', %r10b
  236.     CMP $0, mask(,%r10d,4)
  237.     JE noOperation
  238.     MOV $9, %r11
  239.     SUB %r10, %r11
  240.     ADD $'0', %r11
  241.     MOVB %r11b, (%r8)
  242.     INC %rdi
  243.     INC %r8
  244.     JMP mainLoop
  245.  
  246. substitute:
  247.     XOR %r10, %r10
  248.     MOVB (%rdi), %r10b
  249.     SUBB $'0', %r10b
  250.     CMP $0, mask(,%r10d,4)
  251.     JE noOperation
  252.     MOVB %cl, (%r8)
  253.     INC %rdi
  254.     INC %r8
  255.     JMP mainLoop
  256.  
  257. #MAX_IND (POPRAWKA)
  258. .text
  259. .type max_1_ind, @function
  260. .global max_1_ind
  261.  
  262. #rdi  -tab
  263. #rsi - n
  264. #rdx - even_count
  265. #rcx - neg-count
  266. max_1_ind:
  267.     PUSH %rbx
  268.     PUSH %rdx
  269.     PUSH %rdi
  270.     MOV $0, %r10 # even_count
  271.     MOVB $0, (%rcx)
  272.     MOV $0, %rbx #max 1 counter
  273.     MOV %rsi, %r8
  274. loop:
  275.     MOVQ (%rdi), %rax
  276.     CMP $0, %rax
  277.     JL negative
  278. afterNeg:
  279.     MOVQ (%rdi), %rax
  280.     XOR %rdx, %rdx
  281.     XOR %r12, %r12
  282.     MOV $2, %r12
  283.     IDIV %r12d
  284.     CMP $0, %rdx
  285.     JE even
  286. afterEven:
  287.     XOR %rax, %rax
  288.     MOV (%rdi), %rax
  289.     JMP count1
  290. afterCount:
  291.     DEC %rsi
  292.     ADD $8, %rdi
  293.     CMP $0, %rsi
  294.     JNE loop
  295.     POP %rdi
  296.     POP %rdx
  297.     POP %rbx
  298.     MOV %r11, %rax
  299.     MOVQ %r10, (%rdx)
  300.     RET
  301.  
  302. negative:
  303.     XOR %r12, %r12
  304.     MOVQ (%rcx), %r12
  305.     INC %r12
  306.     MOVQ %r12, (%rcx)
  307.     JMP afterNeg
  308.  
  309. even:
  310.     INC %r10
  311.     JMP afterEven
  312.  
  313. count1:
  314.     XOR %r9, %r9
  315.     XOR %r12, %r12
  316.     MOV $0, %r9
  317.     MOV $0, %r12 # how many 1
  318. countLoop:
  319.     PUSH %rax
  320.     AND $1, %rax
  321.     CMP $0, %rax
  322.     JE zero
  323.     INC %r12
  324. zero:
  325.     POP %rax
  326.     SHR $1, %rax
  327.     INC %r9
  328.     CMP $64, %r9
  329.     JNE countLoop
  330.     CMP %r12, %rbx
  331.     JG afterCount
  332.     MOV %r12, %rbx
  333.     MOVQ (%rdi), %r11
  334.     JMP afterCount
  335.  
  336. #GENERATE_STRING
  337. .data
  338.     s:
  339.     .byte 0
  340.  
  341. .type generate_str,@function
  342. .global generate_str
  343. #rdi->rsi->rdx->rcx
  344.  
  345. generate_str:
  346.     MOV %rdi, s
  347.     CMP $0, %rcx
  348.     JE theSameLetter
  349.     JMP changeLetter
  350.  
  351. theSameLetter:
  352.     CMP $0, %rdx
  353.     JE exit
  354.     MOV %rsi, (%rdi)
  355.     INC %rdi
  356.     DEC %rdx
  357.     JMP theSameLetter
  358.  
  359. changeLetter
  360.     CMP $0, %rdx
  361.     JE exit
  362.     MOV %rsi, (%rdi)
  363.     INC %rsi
  364.     INC %rdi
  365.     DEC %rdx
  366.     JMP changeLetter
  367.  
  368. exit:
  369.     MOV s, %rax
  370.     RET
  371.  
  372. #NA ZMIANE LITERY
  373. .data
  374.     buf:
  375.     .byte   0
  376.  
  377. .text
  378. .type fun,@function
  379. .global fun
  380.  
  381. #rdi - pierwszy argument
  382. #rsi - drugi argument
  383. #rdx - trzeci argument
  384.  
  385. fun:
  386.     MOV %rdi, buf
  387.  
  388. takeFromFirst:
  389.     MOVB (%rsi), %al
  390.     CMPB $0, %al
  391.     JE isEndOfSecond
  392.     MOV %al, (%rdi)
  393.     INC %rdi
  394.     INC %rsi
  395.     JMP takeFromSecond
  396.  
  397. takeFromSecond:
  398.     MOVB (%rdx), %al
  399.     CMPB $0, %al
  400.     JE isEndOfFirst
  401.     MOV %al, (%rdi)
  402.     INC %rdi
  403.     INC %rdx
  404.     JMP takeFromFirst
  405.  
  406. isEndOfFirst:
  407.     MOV (%rsi),%al
  408.     CMPB $0, %al
  409.     JE exit
  410.     JMP takeFromFirst
  411.  
  412. isEndOfSecond:
  413.     MOV (%rdx),%al
  414.     CMPB $0, %al
  415.     JE exit
  416.     JMP takeFromSecond
  417.  
  418. exit:
  419.     MOV buf, %rax
  420.     RET
  421.  
  422. #USUWANIE SPACJI
  423. .text
  424. .type transform, @function
  425. .global transform
  426.  
  427. #rdi - string pointer
  428. #rsi - change
  429.  
  430. #0 - no space
  431. #1 - space found
  432.  
  433. transform:
  434.     MOV %rdi, %r10 #beginning
  435.     MOV %rdi, %r9 #second pointer
  436.     MOV $0, %r11
  437. loop:  
  438.     CMP $' ', (%rdi)
  439.     JE space
  440.     MOV $0, %r11
  441.     CMP $1, %rsi
  442.     JE transformToUpperCase
  443. rewrite:
  444.     MOV (%rdi), %al
  445.     MOV %al, (%r9)
  446.     INC %r9
  447. afterSpace:
  448.     INC %rdi
  449.     CMP $0, (%rdi)
  450.     JNE loop
  451.     MOV %r10, %rax
  452.     RET
  453. space:
  454.     CMP $0, %r11
  455.     JE moveSpace
  456.     JMP afterSpace
  457.  
  458. moveSpace:
  459.     MOV $1, %r11
  460.     JMP rewrite
  461.  
  462. transformToUpperCase:
  463.     CMPB $'a', (%rdi)
  464.     JB rewrite
  465.     CMPB $'z', (%rdi)
  466.     JA rewrite
  467.     MOV (%rdi), %al
  468.     SUB $32, %al
  469.     MOV %al, (%r9)
  470.     INC %r9
  471.     JMP afterSpace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement