Skip to content

Instantly share code, notes, and snippets.

@3dgoose
Created April 14, 2024 10:42
Show Gist options
  • Save 3dgoose/94c31d27d3309b391145ab9b8a8787a6 to your computer and use it in GitHub Desktop.
Save 3dgoose/94c31d27d3309b391145ab9b8a8787a6 to your computer and use it in GitHub Desktop.
; Define section for data and code
section .data
data db 42, -6, 13, -7, 99, 2 ; Replace with your desired array elements
data_size equ $ - data ; Calculate array size
section .text
global _start
_start:
; Initialize registers
xor eax, eax ; Clear accumulator (eax)
mov ecx, data_size ; Load array size into counter (ecx)
mov edx, data ; Load array address into data pointer (edx)
; Find majority element
find_loop:
cmp ecx, 0 ; Check if all elements processed
je found ; Jump if yes
mov ebx, [edx] ; Load current element into temporary register (ebx)
cmp eax, ebx ; Compare with current majority candidate (eax)
je inc_count ; Jump if equal, increment count
dec ecx ; Decrement counter
add edx, 4 ; Move data pointer to next element (4 bytes per integer)
cmp eax, 0 ; Check if current majority candidate has 0 count
jg next_candidate ; Jump if greater than 0, continue with current candidate
; Reset majority candidate and count
mov eax, ebx ; Set new majority candidate
mov ecx, 1 ; Reset count to 1
next_candidate:
jmp find_loop ; Continue loop
inc_count:
inc eax ; Increment count for current majority candidate
found:
; Check if current majority candidate is indeed the majority element
cmp ecx, 1 ; Check if count is 1 (no majority element)
je no_majority ; Jump if yes
; Print result (assuming you have a printing function)
; ... call print_function(eax) ...
no_majority:
; ... print "No majority element found" ...
; Exit program
mov eax, 1
int 0x80
; Define print function (implementation omitted)
; ... print_function proc ...
; ... endp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment