text # Don't Print x Challenge: # Write and call a function that performs as per the following pseudo code: # print(char arg1, char arg2, char arg3, char arg4, char arg5, char arg6, char arg7, char arg8, char* message) # loop through arg1 - arg8 # if arg != 'x' # print "This is:arg not x" # # Your code should comply with AMD64 System V ABI # The only global variable used should be the string array # Test your function by passing in chars with at least one x # Your code only needs to work with lower case chars # Hint a string is just an array of bytes, you can use 'c' to specify a char # Example mov rax, 'c' # Start of function code: .intel_syntax noprefix .text .section .data message: .string "This is:% not x\n" .equ msg_len, .-message-1 .equ spc_to_sub, 8 .text .globl print .type print, @function print: push rbp mov rbp, rsp # Setting up call stack sub rsp, 72 mov QWORD PTR -8[rbp], rdi # mov first argument (rdi) into stack mov QWORD PTR -16[rbp], rsi # mov second argument (rsi) into stack mov QWORD PTR -24[rbp], rdx # mov third argument (rdx) into stack mov QWORD PTR -32[rbp], rcx # mov fourth argument (rcx) into stack mov QWORD PTR -40[rbp], r8 # mov fifth argument (r8) into stack mov QWORD PTR -48[rbp], r9 # mov 6th argument (r9) into stack mov rax, QWORD PTR 16[rbp] # mov 7th argument off of arg stack mov QWORD PTR -56[rbp], rax # mov 7th argument into call stack mov rax, QWORD PTR 24[rbp] # mov 8th argument off of arg stack mov QWORD PTR -64[rbp], rax # mov 8th argument into call stack mov rax, QWORD PTR 32[rbp] # mov 9th argument off of arg stack mov QWORD PTR -72[rbp], rax # mov 9th argument into call stack push rbx # Preserve rbx to stack(callee must save rbx) mov rbx, rbp # rbx to be used as base pointer for array of chars sub rbx, 8 # move it to start at first arg mov rcx, 0 # loop counter at 0 mov rdi, 1 # setting fd to STDOUT (1) start_loop: # Start of function code to call write syscall cmp [rbx], QWORD PTR 'x' # check if char is an x je below_print # if it is, jump below the print mov rax, 0x01 # 0x01 is write syscall mov edx, msg_len # length fo string to print mov rsi, QWORD PTR -72[rbp] # pointer to start of message to print mov r9, [rbx] # mov letter into string mov [rsi+spc_to_sub], r9b # mov letter into string push rcx # cx is caller preserved so must preserve before calling syscall syscall pop rcx # restoring rcx cmp rax, 1 # checking if syscall wrote bytes jl return # if it didn't and sent an error return that below_print: inc cx # increment loop counter sub rbx, 8 # mov array base pointer to next char cmp rcx, 8 # check if at end of foor loop jb start_loop # start loop back over if not at end of for mov rax, 0 # set return to 0, no error return: pop rbx # restore rbx for caller mov rsp, rbp pop rbp ret .size print, .-print .text .globl main .type main, @function # Call order goes rdi, rsi, rdx, rcx, r8, r9, then stack main: push rbp mov rbp, rsp # mov first 6 chars using registers rdi, rsi, rdx, rcx, r8, r9 mov rdi, 'a' mov rsi, 'b' mov rdx, 'c' mov rcx, 'd' mov r8, 'e' mov r9, 'f' sub rsp, 8 # align stack pointer lea rax, message[rip] # arg 9 into stack push rax # arg 9 into stack mov rax, 'z' # arg 8 into stack push rax # arg 8 into stack mov rax, 'x' # arg 7 into stack push rax # arg 7 into stack call print add rsp, 32 # clean up stack args mov rsp, rbp pop rbp ret .size main, .-main .section .note.GNU-stack,"",@progbits