Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.10 KB | None | 0 0
  1. %include "simple_io.inc"
  2. global asm_main
  3. extern rperm
  4.  
  5. section .data
  6. prompt1: db "if you want to swap, enter a,b",10,0
  7. prompt2: db "if you want to end, enter 0: ",0
  8. a1: db "first coordinate not 1..8",10,0
  9. a2: db "you need a comma to sperate your two numbers",10,0
  10. a3: db "you're missing your 2nd number",10,0
  11. botOf8: db "..+------+",0
  12. botOf7: db "..+-----+.",0
  13. botOf6: db "...+----+.",0
  14. botOf5: db "...+---+..",0
  15. botOf4: db "....+--+..",0
  16. botOf3: db "....+-+...",0
  17. botOf2: db ".....++...",0
  18. botOf1: db ".....+....",0
  19. mid8: db " + +",0
  20. mid7: db " + + ",0
  21. mid6: db " + + ",0
  22. mid5: db " + + ",0
  23. mid4: db " + + ",0
  24. mid3: db " + + ",0
  25. mid2: db " ++ ",0
  26. mid1: db " + ",0
  27. topOf8: db " +------+",0
  28. topOf7: db " +-----+ ",0
  29. topOf6: db " +----+ ",0
  30. topOf5: db " +---+ ",0
  31. topOf4: db " +--+ ",0
  32. topOf3: db " +-+ ",0
  33. topOf2: db " ++ ",0
  34. topOf1: db " + ",0
  35. numLeft: db " ",0
  36. numRight: db " ",0
  37. blankSpaces: db " ",0
  38. section .bss
  39. array: resq 8
  40.  
  41.  
  42. section .text
  43.  
  44.  
  45.  
  46.  
  47. display:
  48. enter 0,0
  49. saveregs
  50. ;;Starting to print the stuff at the top line
  51.  
  52. mov r13, [rbp+24] ;; array
  53. mov r12, [rbp+32] ;; size
  54. ; mov r13, array
  55. ; mov r12, qword 8
  56. mov rcx, 9 ;r11 ;rcx=8 this is loop counter for the levels starting at 8 ending at 0
  57. printLine:
  58. mov r14, rcx
  59. sub r14, 1
  60. push r14 ;;level
  61. push r13 ;;array
  62. sub rsp, 8 ;;adjusting
  63. call createLine
  64. add rsp, 8
  65. pop r13
  66. pop r14
  67.  
  68. call print_nl
  69.  
  70. loop printLine
  71.  
  72. restoregs
  73. leave
  74. ret
  75.  
  76. ;befoire this you gotta push the three parameters on to the stack
  77. ;so we'll have 3 paramers, level, size, line
  78. ; when passing, pass in opposite order. so push line, push size, push level
  79. add2line:
  80. enter 0,0
  81. saveregs
  82.  
  83.  
  84. mov r15, qword [rbp+24] ;level
  85. mov r14, qword [rbp+32] ;size (array)
  86.  
  87.  
  88. ;now we want our first if statement from
  89. ;if level == 1
  90. cmp r15, 1
  91. je cond1
  92.  
  93. ;;if level is greater than 1
  94. cmp r15, 1
  95. jg cond2
  96.  
  97. ;; if level = 0 we have to print the numbers at the bottom
  98. cmp r15, 0
  99. je cond3
  100.  
  101. jmp add2lineEnd
  102.  
  103.  
  104. add2lineEnd:
  105. restoregs
  106. leave
  107. ret
  108. ;;must clean up too ya
  109. ;;add rsp, m where m = 8 * number of paramerters after function call
  110.  
  111.  
  112. cond1:
  113. ;if size = 8
  114. cmp r14, 8
  115. je b8
  116.  
  117. ;if size = 7
  118. cmp r14, 7
  119. je b7
  120.  
  121. ;if size = 6
  122. cmp r14, 6
  123. je b6
  124.  
  125. ;if size = 5
  126. cmp r14, 5
  127. je b5
  128.  
  129. ;if size = 4
  130. cmp r14, 4
  131. je b4
  132.  
  133. ;if size = 3
  134. cmp r14, 3
  135. je b3
  136.  
  137. ;if size = 2
  138. cmp r14, 2
  139. je b2
  140.  
  141. ;if size = 1
  142. cmp r14, 1
  143. je b1
  144.  
  145. jmp add2lineEnd
  146.  
  147. b8:
  148. mov rax, botOf8 ;put string
  149. call print_string
  150. jmp add2lineEnd
  151.  
  152. b7:
  153. mov rax, botOf7 ;put string
  154. call print_string
  155. jmp add2lineEnd
  156. b6:
  157. mov rax, botOf6 ;put string
  158. call print_string
  159. jmp add2lineEnd
  160. b5:
  161. mov rax, botOf5 ;put string
  162. call print_string
  163. jmp add2lineEnd
  164. b4:
  165. mov rax, botOf4 ;put string
  166. call print_string
  167. jmp add2lineEnd
  168. b3:
  169. mov rax, botOf3 ;put string
  170. call print_string
  171. jmp add2lineEnd
  172. b2:
  173. mov rax, botOf2 ;put string
  174. call print_string
  175. jmp add2lineEnd
  176. b1:
  177. mov rax, botOf1 ;put string
  178. call print_string
  179. jmp add2lineEnd
  180.  
  181. ;;level greater than 1
  182. cond2:
  183. ;;if size is less than level
  184. cmp r14, r15
  185. jl spaces
  186.  
  187. ;;If the size and level are equal
  188. cmp r14, r15
  189. je topLines
  190.  
  191. cmp r14, r15
  192. jg midLines
  193.  
  194. jmp add2lineEnd
  195.  
  196. spaces:
  197. mov rax, blankSpaces
  198. call print_string
  199. jmp add2lineEnd
  200.  
  201. topLines:
  202. ;;if size is 8,7,6,5,4,3,2,1
  203. cmp r14, 8
  204. je t8
  205.  
  206. cmp r14, 7
  207. je tt7
  208.  
  209. cmp r14, 6
  210. je tt6
  211.  
  212. cmp r14, 5
  213. je t5
  214.  
  215. cmp r14, 4
  216. je t4
  217.  
  218. cmp r14, 3
  219. je t3
  220.  
  221. cmp r14, 2
  222. je t2
  223.  
  224. cmp r14, 1
  225. je t1
  226.  
  227. jmp add2lineEnd
  228.  
  229. t8:
  230. mov rax, topOf8
  231. call print_string
  232. jmp add2lineEnd
  233. tt7:
  234. mov rax, topOf7
  235. call print_string
  236. jmp add2lineEnd
  237. tt6:
  238. mov rax, topOf6
  239. call print_string
  240. jmp add2lineEnd
  241. t5:
  242. mov rax, topOf5
  243. call print_string
  244. jmp add2lineEnd
  245. t4:
  246. mov rax, topOf4
  247. call print_string
  248. jmp add2lineEnd
  249. t3:
  250. mov rax, topOf3
  251. call print_string
  252. jmp add2lineEnd
  253. t2:
  254. mov rax, topOf2
  255. call print_string
  256. jmp add2lineEnd
  257. t1:
  258. mov rax, topOf1
  259. call print_string
  260. jmp add2lineEnd
  261.  
  262. midLines:
  263. cmp r14,8
  264. je m8
  265.  
  266. cmp r14,7
  267. je m7
  268.  
  269. cmp r14,6
  270. je m6
  271.  
  272. cmp r14,5
  273. je m5
  274.  
  275. cmp r14,4
  276. je m4
  277.  
  278. cmp r14,3
  279. je m3
  280.  
  281. cmp r14,2
  282. je m2
  283.  
  284. cmp r14,1
  285. je m1
  286. jmp add2lineEnd
  287.  
  288.  
  289. m8:
  290. mov rax, mid8
  291. call print_string
  292. jmp add2lineEnd
  293. m7:
  294. mov rax, mid7
  295. call print_string
  296. jmp add2lineEnd
  297. m6:
  298. mov rax, mid6
  299. call print_string
  300. jmp add2lineEnd
  301. m5:
  302. mov rax, mid5
  303. call print_string
  304. jmp add2lineEnd
  305. m4:
  306. mov rax, mid4
  307. call print_string
  308. jmp add2lineEnd
  309. m3:
  310. mov rax, mid3
  311. call print_string
  312. jmp add2lineEnd
  313. m2:
  314. mov rax, mid2
  315. call print_string
  316. jmp add2lineEnd
  317. m1:
  318. mov rax, mid1
  319. call print_string
  320. jmp add2lineEnd
  321.  
  322. cond3:
  323. mov rax, numLeft
  324. call print_string
  325. mov rax, r14
  326. call print_int
  327. mov rax, numRight
  328. call print_string
  329. jmp add2lineEnd
  330.  
  331.  
  332.  
  333.  
  334.  
  335. ;;when using create line we gonna push level, then array
  336. createLine:
  337. enter 0,0
  338. saveregs
  339.  
  340. ;adjust by sub "rsp, 8" for un even var
  341.  
  342. mov rdx, [rbp+24] ;;array
  343. mov r12, [rbp+32] ;;level
  344. ;we have countdown array from 8,0
  345.  
  346. mov r13, qword 0 ;;counter 0 to 7 (56 because we multiply by 8) (so the indicides of the array)
  347. myLoop1:
  348. cmp r13, qword 56
  349. ja endLoop
  350.  
  351. mov r14, rdx
  352. add r14, qword r13
  353. mov r15, [r14]
  354.  
  355.  
  356. push r15 ;;pushing the array[i]
  357. push r12 ;;pushing the level
  358. sub rsp, 8 ;;aligning
  359.  
  360. call add2line
  361.  
  362. add rsp, 8 ;;celaning up
  363. pop r12
  364. pop r15
  365.  
  366.  
  367.  
  368.  
  369. add r13, qword 8 ;;increasing counter
  370. jmp myLoop1
  371. endLoop:
  372.  
  373. restoregs
  374. leave
  375. ret
  376.  
  377. asm_main:
  378. enter 0,0
  379. saveregs
  380.  
  381. mov rdi, array ;1st param for rperm
  382. mov rsi, qword 8 ;2nd param for rperm
  383. call rperm
  384.  
  385. testFlag:
  386.  
  387. push 8
  388. push array
  389. sub rsp, 8
  390. call display
  391. add rsp, 24
  392.  
  393.  
  394.  
  395. prompt:
  396. mov rax, prompt1
  397. call print_string
  398. mov rax, prompt2
  399. call print_string
  400.  
  401.  
  402. read:
  403. call read_char ;;reading the input
  404. cmp al, '0' ;;checking if its a 0
  405. je asm_main_end ;;if it is then jump to end
  406.  
  407. ;;reading for first char, checkign for error less than 1 or greater than 8
  408. cmp al, '1'
  409. jb error1
  410. cmp al, '8'
  411. ja error1
  412.  
  413. ;;reading and storing first character in r12b
  414. mov r12, 0
  415. mov r12b, al
  416. sub r12b, '0'
  417.  
  418. ;;checking if its a comma, if it isn't jump to the error message
  419. call read_char
  420. cmp al, ','
  421. jne error2
  422.  
  423. ;;checking if its less than one, greater than 8 or if the user wants to end the code
  424. call read_char
  425. cmp al, '0'
  426. je asm_main_end
  427. cmp al, '1'
  428. jb error3
  429. cmp al, '8'
  430. ja error3
  431.  
  432. ;;storing the second character into r13b
  433. mov r13, 0
  434. mov r13b, al
  435. sub r13b, '0'
  436.  
  437. ;;storing the array in the r14 register
  438. mov r14, array
  439.  
  440. LOOP1:
  441. cmp [r14], r12
  442. je LOOP2
  443. add r14, 8
  444. jmp LOOP1
  445.  
  446. LOOP2:
  447. mov r15, array
  448. LOOP3:
  449. cmp [r15], r13
  450. je LOOP4
  451. add r15, 8
  452. jmp LOOP3
  453. LOOP4:
  454. mov [r14], r13
  455. mov [r15], r12
  456.  
  457. push 8
  458. push array
  459. sub rsp, 8
  460. call display2
  461. add rsp, 24
  462.  
  463.  
  464. jmp L1
  465. ; jmp prompt
  466. jmp testFlag
  467. jmp asm_main_end
  468.  
  469. error1:
  470. call print_nl
  471. mov rax, a1
  472. call print_string
  473. ;;empty input buffer
  474. jmp testFlag
  475. ; jmp L1
  476. ; jmp prompt
  477. error2:
  478. call print_nl
  479. mov rax, a2
  480. call print_string
  481. ;;empty input buffer
  482. jmp testFlag
  483. ; jmp L1
  484. ; jmp prompt
  485.  
  486. error3:
  487. call print_nl
  488. mov rax, a3
  489. call print_string
  490. ;;empty input buffer
  491. jmp testFlag
  492. ; jmp L1
  493. ; jmp prompt
  494.  
  495. L1:
  496. cmp al, 10
  497. je L2
  498. call read_char
  499. jmp L1
  500. L2:
  501. jmp prompt
  502.  
  503.  
  504.  
  505. asm_main_end:
  506. restoregs
  507. leave
  508. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement