Skip to content

Instantly share code, notes, and snippets.

@carld
Created September 10, 2016 07:34
Show Gist options
  • Save carld/ee6015d56c80298080f85fcbba1dc4c1 to your computer and use it in GitHub Desktop.
Save carld/ee6015d56c80298080f85fcbba1dc4c1 to your computer and use it in GitHub Desktop.
string equals in x86_64 nasm assembly
section .text
global start
start:
mov rsi, s1 ; esi = &s1
mov rdi, s2 ; edi = &s2
xor rdx, rdx ; edx = 0
loop:
mov al, [rsi + rdx]
mov bl, [rdi + rdx]
inc rdx
cmp al, bl ; compare two current characters
jne not_equal ; not equal
cmp al, 0 ; at end?
je equal ; end of both strings
jmp loop ; equal so far
not_equal:
mov rdi, 1
jmp exit
equal:
mov rdi, 0
jmp exit
exit:
mov rax, 0x2000001 ; sys_exit
; mov rdi, 0
syscall
section .data
s1: db "string one", 0
s2: db "string on1", 0
@0x4ka5h
Copy link

0x4ka5h commented Apr 21, 2021

:">?<>?<>>??

@alexaragao
Copy link

This code has an error at line 23. mov rax, 0x2000001 will raise segmentation fault (core dump). Use mov rax, 60 instead. 60 is the code of the system call SYS_EXIT.

Copy link

ghost commented Sep 15, 2021

This code has an error at line 23. mov rax, 0x2000001 will raise segmentation fault (core dump). Use mov rax, 60 instead. 60 is the code of the system call SYS_EXIT.

mov rax, 0x2000001 is the syscall for exit in assembly on macos.

@milobanks
Copy link

You can also use the cmps instruction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment