Skip to content

Latest commit

 

History

History
79 lines (52 loc) · 1.02 KB

File metadata and controls

79 lines (52 loc) · 1.02 KB

Question 19

Can you spot a bug or two in Listing strlen_bug1.asm? When will they occur?

global _start

section .data

test_string: db "abcdef", 0

section .text

strlen:                       
.loop:                        
cmp byte [rdi+r13], 0     
je .end                   
inc r13                   
jmp .loop
.end:
mov rax, r13
ret                       

_start:
mov rdi, test_string
call strlen
mov rdi, rax

mov rax, 60
syscall

Answer

  • r13 is not zeroed before usage
  • r13 should be saved before usage and restored before ret
global _start

section .data

test_string: db "abcdef", 0

section .text

strlen:                       
push r13                ;
xor r13, r13            ;
.loop:                        
cmp byte [rdi+r13], 0     
je .end                   
inc r13                   
jmp .loop
.end:
mov rax, r13
pop r13                 ;
ret                       

_start:
mov rdi, test_string
call strlen
mov rdi, rax

mov rax, 60
syscall 

prev +++ next