# nasm – CodeWars – Assembly Language – Check List For Value

I’m doing assembly language problems on CodeWars, a website with practice problems.

## Problem

https://www.codewars.com/kata/545991b4cbae2a5fda000158/train/nasm

Create a method that accepts a list and an item, and returns true if the item belongs to the list, otherwise false.

## Solution In C

To give you an idea what the assembly code will be doing.

``````#include <stdbool.h>
#include <stddef.h>

bool include(const int* arr, size_t size, int item)
{
int i = 0;
loop:
if ( i < size ) {
if ( arr(i) == item ) {
return true;
}
i++;
goto loop;
}
return false;
}
``````

## Solution In NASM Assembly (Linux x64)

CodeWars provided the 6 lines at the top.

``````SECTION .text
global include
include:

; bool include(const int* arr, size_t size, int item)
; sizeof(int) = 4 bytes (32bit)
; sizeof(size_t) = 8 bytes (64bit)

;rdi = &arr    pointer, 8 bytes
;   arr(i)          signed int, 4 bytes (dd)
;rsi = size    size_t, unsigned int, 8 bytes
;edx = item    signed int, 4 bytes

; Avoid using registers that we need to preserve (RBX, RBP, R12-R15). Else we'd have to push and pop them onto the stack.

mov rcx, 0      ; unsigned int i = 0;
loop1:
cmp rcx, rsi    ; if ( i < size ) {
jae skip_loop

mov r8d, (rdi + 4 * rcx)        ; make a temp variable so we can see this in step debugging
cmp edx, r8d    ; if ( arr(i) == item ) {
jne skip_if
mov rax, 1      ; return true;
ret

skip_if:
inc rcx         ; i++;
jmp loop1

skip_loop:
mov rax, 0      ; return false;
ret
``````

## Questions

I’m brand new to assembly. Any feedback on patterns and best practices would be appreciated. For example

• Is there a standard pattern to use when writing loops?
• Is there a standard pattern to use when writing if/elseif/else?
• Are there better word choices and formatting for the labels?