Skip to content

Commit

Permalink
Add mem compare
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillerDragon committed Aug 25, 2024
1 parent bc5f585 commit 1c8bfb4
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 6 deletions.
36 changes: 36 additions & 0 deletions src/system.asm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,42 @@ str_length:
pop_registers_keep_rax
ret

mem_comp:
; mem_comp [rax] [rdi] [rsi]
; rax = memory region a to compare
; rdi = memory region b to compare
; rsi = amount of bytes to compare
; returns boolean into zero flag
; "je" will jump if they match
push_registers

mov rcx, 0

.mem_comp_byte_loop:
mov r8b, byte [rdi+rcx]
cmp byte [rax+rcx], r8b
jne .mem_comp_no_match
inc rcx
cmp rcx, rsi
jle .mem_comp_byte_loop

.mem_comp_match:
; ugly hack to flip the zero flag like in is_rax_flag
; set EQUAL FLAG
mov al, 0
cmp al, 0
jmp .mem_comp_end

.mem_comp_no_match:
; ugly hack to flip the zero flag like in is_rax_flag
; set NOT EQUAL FLAG
mov al, 0
cmp al, 1

.mem_comp_end:
pop_registers
ret

str_comp:
; str_comp [rax] [rdi]
; rax = string to compare
Expand Down
51 changes: 47 additions & 4 deletions tests/assert.asm
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,45 @@ assert_ok:
call assert_ok
%endmacro

%macro assert_mem_eq 4
; assert_mem_eq [expected] [actual] [size] [__LINE__]
push_registers

mov rbp, rsp
sub rsp, 24
mov qword [rbp-24], %1
mov qword [rbp-16], %2
mov qword [rbp-8], %3

mov rax, [rbp-24]
mov rdi, [rbp-16]
mov rsi, [rbp-8]
call mem_comp
je %%assert_ok

assert_trace %3
print_label s_assert_error
print_label s_assert_expected
mov rax, [rbp-24]
mov rdi, [rbp-8]
call print_hexdump
call print_newline
print_label s_assert_actual
mov rax, [rbp-16]
mov rdi, [rbp-8]
call print_hexdump
call print_newline

exit 1

%%assert_ok:
call assert_ok

mov rsp, rbp

pop_registers
%endmacro

%macro assert_str_eq 3
; assert_str_eq [expected] [actual] [__LINE__]
push_registers
Expand All @@ -330,12 +369,16 @@ assert_ok:

assert_trace %3
print_label s_assert_error
print " expected: '"
print_label s_assert_expected
call print_single_quote
print_c_str [rbp-16]
puts "'"
print " actual: '"
call print_single_quote
call print_newline
print_label s_assert_actual
call print_single_quote
print_c_str [rbp-8]
puts "'"
call print_single_quote
call print_newline

exit 1

Expand Down
50 changes: 50 additions & 0 deletions tests/mem_comp_test.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
%include "tests/assert.asm"

_start:
init_test __FILE__

_test_mem_comp_match:
; mem_comp [rax] [rdi] [rsi]
; rax = memory region a to compare
; rdi = memory region b to compare
; rsi = amount of bytes to compare
; returns boolean into zero flag
; "je" will jump if they match

mov byte [generic_buffer_512+0], 0xff
mov byte [generic_buffer_512+1], 0xaa

mov byte [generic_buffer_16+0], 0xff
mov byte [generic_buffer_16+1], 0xaa

mov rax, generic_buffer_512
mov rdi, generic_buffer_16
mov rsi, 2
call mem_comp
assert_is_true __LINE__

_test_mem_comp_no_match:
mov byte [generic_buffer_512+0], 0xaa
mov byte [generic_buffer_512+1], 0xaa

mov byte [generic_buffer_16+0], 0xff
mov byte [generic_buffer_16+1], 0xaa

mov rax, generic_buffer_512
mov rdi, generic_buffer_16
mov rsi, 2
call mem_comp
assert_is_false __LINE__

_test_assert_macro:

mov byte [generic_buffer_512+0], 0xff
mov byte [generic_buffer_512+1], 0xaa

mov byte [generic_buffer_16+0], 0xff
mov byte [generic_buffer_16+1], 0xaa

assert_mem_eq generic_buffer_512, generic_buffer_16, 2, __LINE__

end_test __LINE__

3 changes: 1 addition & 2 deletions tests/sockaddr_to_str_test.asm
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ section .text
_start:
init_test __FILE__

_test_str_startswith_basic_match:

_test_sockaddr_to_str:
; sockaddr_to_str [rax] [rdi]
; rax = pointer to sockaddr struct
; rdi = output buffer
Expand Down

0 comments on commit 1c8bfb4

Please sign in to comment.