Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %include "simple_io.inc"
- global asm_main
- extern rperm
- section .data
- prompt1: db "if you want to swap, enter a,b",10,0
- prompt2: db "if you want to end, enter 0: ",0
- a1: db "first coordinate not 1..8",10,0
- a2: db "you need a comma to sperate your two numbers",10,0
- a3: db "you're missing your 2nd number",10,0
- botOf8: db "..+------+",0
- botOf7: db "..+-----+.",0
- botOf6: db "...+----+.",0
- botOf5: db "...+---+..",0
- botOf4: db "....+--+..",0
- botOf3: db "....+-+...",0
- botOf2: db ".....++...",0
- botOf1: db ".....+....",0
- mid8: db " + +",0
- mid7: db " + + ",0
- mid6: db " + + ",0
- mid5: db " + + ",0
- mid4: db " + + ",0
- mid3: db " + + ",0
- mid2: db " ++ ",0
- mid1: db " + ",0
- topOf8: db " +------+",0
- topOf7: db " +-----+ ",0
- topOf6: db " +----+ ",0
- topOf5: db " +---+ ",0
- topOf4: db " +--+ ",0
- topOf3: db " +-+ ",0
- topOf2: db " ++ ",0
- topOf1: db " + ",0
- numLeft: db " ",0
- numRight: db " ",0
- blankSpaces: db " ",0
- section .bss
- array: resq 8
- section .text
- display:
- enter 0,0
- saveregs
- ;;Starting to print the stuff at the top line
- mov r13, [rbp+24] ;; array
- mov r12, [rbp+32] ;; size
- ; mov r13, array
- ; mov r12, qword 8
- mov rcx, 9 ;r11 ;rcx=8 this is loop counter for the levels starting at 8 ending at 0
- printLine:
- mov r14, rcx
- sub r14, 1
- push r14 ;;level
- push r13 ;;array
- sub rsp, 8 ;;adjusting
- call createLine
- add rsp, 8
- pop r13
- pop r14
- call print_nl
- loop printLine
- restoregs
- leave
- ret
- ;befoire this you gotta push the three parameters on to the stack
- ;so we'll have 3 paramers, level, size, line
- ; when passing, pass in opposite order. so push line, push size, push level
- add2line:
- enter 0,0
- saveregs
- mov r15, qword [rbp+24] ;level
- mov r14, qword [rbp+32] ;size (array)
- ;now we want our first if statement from
- ;if level == 1
- cmp r15, 1
- je cond1
- ;;if level is greater than 1
- cmp r15, 1
- jg cond2
- ;; if level = 0 we have to print the numbers at the bottom
- cmp r15, 0
- je cond3
- jmp add2lineEnd
- add2lineEnd:
- restoregs
- leave
- ret
- ;;must clean up too ya
- ;;add rsp, m where m = 8 * number of paramerters after function call
- cond1:
- ;if size = 8
- cmp r14, 8
- je b8
- ;if size = 7
- cmp r14, 7
- je b7
- ;if size = 6
- cmp r14, 6
- je b6
- ;if size = 5
- cmp r14, 5
- je b5
- ;if size = 4
- cmp r14, 4
- je b4
- ;if size = 3
- cmp r14, 3
- je b3
- ;if size = 2
- cmp r14, 2
- je b2
- ;if size = 1
- cmp r14, 1
- je b1
- jmp add2lineEnd
- b8:
- mov rax, botOf8 ;put string
- call print_string
- jmp add2lineEnd
- b7:
- mov rax, botOf7 ;put string
- call print_string
- jmp add2lineEnd
- b6:
- mov rax, botOf6 ;put string
- call print_string
- jmp add2lineEnd
- b5:
- mov rax, botOf5 ;put string
- call print_string
- jmp add2lineEnd
- b4:
- mov rax, botOf4 ;put string
- call print_string
- jmp add2lineEnd
- b3:
- mov rax, botOf3 ;put string
- call print_string
- jmp add2lineEnd
- b2:
- mov rax, botOf2 ;put string
- call print_string
- jmp add2lineEnd
- b1:
- mov rax, botOf1 ;put string
- call print_string
- jmp add2lineEnd
- ;;level greater than 1
- cond2:
- ;;if size is less than level
- cmp r14, r15
- jl spaces
- ;;If the size and level are equal
- cmp r14, r15
- je topLines
- cmp r14, r15
- jg midLines
- jmp add2lineEnd
- spaces:
- mov rax, blankSpaces
- call print_string
- jmp add2lineEnd
- topLines:
- ;;if size is 8,7,6,5,4,3,2,1
- cmp r14, 8
- je t8
- cmp r14, 7
- je tt7
- cmp r14, 6
- je tt6
- cmp r14, 5
- je t5
- cmp r14, 4
- je t4
- cmp r14, 3
- je t3
- cmp r14, 2
- je t2
- cmp r14, 1
- je t1
- jmp add2lineEnd
- t8:
- mov rax, topOf8
- call print_string
- jmp add2lineEnd
- tt7:
- mov rax, topOf7
- call print_string
- jmp add2lineEnd
- tt6:
- mov rax, topOf6
- call print_string
- jmp add2lineEnd
- t5:
- mov rax, topOf5
- call print_string
- jmp add2lineEnd
- t4:
- mov rax, topOf4
- call print_string
- jmp add2lineEnd
- t3:
- mov rax, topOf3
- call print_string
- jmp add2lineEnd
- t2:
- mov rax, topOf2
- call print_string
- jmp add2lineEnd
- t1:
- mov rax, topOf1
- call print_string
- jmp add2lineEnd
- midLines:
- cmp r14,8
- je m8
- cmp r14,7
- je m7
- cmp r14,6
- je m6
- cmp r14,5
- je m5
- cmp r14,4
- je m4
- cmp r14,3
- je m3
- cmp r14,2
- je m2
- cmp r14,1
- je m1
- jmp add2lineEnd
- m8:
- mov rax, mid8
- call print_string
- jmp add2lineEnd
- m7:
- mov rax, mid7
- call print_string
- jmp add2lineEnd
- m6:
- mov rax, mid6
- call print_string
- jmp add2lineEnd
- m5:
- mov rax, mid5
- call print_string
- jmp add2lineEnd
- m4:
- mov rax, mid4
- call print_string
- jmp add2lineEnd
- m3:
- mov rax, mid3
- call print_string
- jmp add2lineEnd
- m2:
- mov rax, mid2
- call print_string
- jmp add2lineEnd
- m1:
- mov rax, mid1
- call print_string
- jmp add2lineEnd
- cond3:
- mov rax, numLeft
- call print_string
- mov rax, r14
- call print_int
- mov rax, numRight
- call print_string
- jmp add2lineEnd
- ;;when using create line we gonna push level, then array
- createLine:
- enter 0,0
- saveregs
- ;adjust by sub "rsp, 8" for un even var
- mov rdx, [rbp+24] ;;array
- mov r12, [rbp+32] ;;level
- ;we have countdown array from 8,0
- mov r13, qword 0 ;;counter 0 to 7 (56 because we multiply by 8) (so the indicides of the array)
- myLoop1:
- cmp r13, qword 56
- ja endLoop
- mov r14, rdx
- add r14, qword r13
- mov r15, [r14]
- push r15 ;;pushing the array[i]
- push r12 ;;pushing the level
- sub rsp, 8 ;;aligning
- call add2line
- add rsp, 8 ;;celaning up
- pop r12
- pop r15
- add r13, qword 8 ;;increasing counter
- jmp myLoop1
- endLoop:
- restoregs
- leave
- ret
- asm_main:
- enter 0,0
- saveregs
- mov rdi, array ;1st param for rperm
- mov rsi, qword 8 ;2nd param for rperm
- call rperm
- testFlag:
- push 8
- push array
- sub rsp, 8
- call display
- add rsp, 24
- prompt:
- mov rax, prompt1
- call print_string
- mov rax, prompt2
- call print_string
- read:
- call read_char ;;reading the input
- cmp al, '0' ;;checking if its a 0
- je asm_main_end ;;if it is then jump to end
- ;;reading for first char, checkign for error less than 1 or greater than 8
- cmp al, '1'
- jb error1
- cmp al, '8'
- ja error1
- ;;reading and storing first character in r12b
- mov r12, 0
- mov r12b, al
- sub r12b, '0'
- ;;checking if its a comma, if it isn't jump to the error message
- call read_char
- cmp al, ','
- jne error2
- ;;checking if its less than one, greater than 8 or if the user wants to end the code
- call read_char
- cmp al, '0'
- je asm_main_end
- cmp al, '1'
- jb error3
- cmp al, '8'
- ja error3
- ;;storing the second character into r13b
- mov r13, 0
- mov r13b, al
- sub r13b, '0'
- ;;storing the array in the r14 register
- mov r14, array
- LOOP1:
- cmp [r14], r12
- je LOOP2
- add r14, 8
- jmp LOOP1
- LOOP2:
- mov r15, array
- LOOP3:
- cmp [r15], r13
- je LOOP4
- add r15, 8
- jmp LOOP3
- LOOP4:
- mov [r14], r13
- mov [r15], r12
- push 8
- push array
- sub rsp, 8
- call display2
- add rsp, 24
- jmp L1
- ; jmp prompt
- jmp testFlag
- jmp asm_main_end
- error1:
- call print_nl
- mov rax, a1
- call print_string
- ;;empty input buffer
- jmp testFlag
- ; jmp L1
- ; jmp prompt
- error2:
- call print_nl
- mov rax, a2
- call print_string
- ;;empty input buffer
- jmp testFlag
- ; jmp L1
- ; jmp prompt
- error3:
- call print_nl
- mov rax, a3
- call print_string
- ;;empty input buffer
- jmp testFlag
- ; jmp L1
- ; jmp prompt
- L1:
- cmp al, 10
- je L2
- call read_char
- jmp L1
- L2:
- jmp prompt
- asm_main_end:
- restoregs
- leave
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement