Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Function to find the largest numeric digit

find_largest_digit:

mov bl, byte [esi] ; Load first digit

.find_loop:

cmp byte [esi], 0 ; Check for null terminator

je .find_done ; If null terminator, exit loop

mov al, byte [esi] ; Load current digit

cmp al, bl ; Compare with largest digit found so far

jg .update_largest ; If greater, update largest digit

inc esi ; Move to next digit

jmp .find_loop

.update_largest:

mov bl, al ; Update largest digit

inc esi ; Move to next digit

jmp .find_loop

.find_done:

mov [largest_digit], bl ; Store largest digit

ret

; Function to subtract each numeric digit from the largest digit

subtract_from_largest:

xor ecx, ecx ; Clear counter register

.subtract_loop:

mov al, byte [esi + ecx] ; Load digit from numeric_array

mov bl, [largest_digit] ; Load largest digit


sub bl, al ; Subtract current digit from largest digit

mov [edi + ecx], bl ; Store result in temp_array

inc ecx ; Increment counter

cmp ecx, 10 ; Check if all digits processed

je .subtract_done ; If all digits processed, exit loop

jmp .subtract_loop

.subtract_done:

ret

; Function to sort array in ascending order (using Bubble Sort)

sort_array:

xor ecx, ecx ; Outer loop counter

.outer_loop:

xor edx, edx ; Inner loop counter

.inner_loop:

mov al, [esi + edx] ; Load current element

cmp al, [esi + edx + 1] ; Compare with next element

jle .no_swap ; If less than or equal, no swap needed

mov bl, [esi + edx + 1] ; Exchange elements

mov [esi + edx + 1], al

mov [esi + edx], bl

.no_swap:

inc edx ; Move to next element

cmp edx, 9 ; Check if all elements processed

You might also like