0% found this document useful (0 votes)
20 views14 pages

Pasm

The assembly code extracts the numeric part of a VUID string and stores it in an array. It finds the largest numeric digit and subtracts each digit from the largest, storing the results in a temporary array. Finally, it sorts the array in ascending order.

Uploaded by

Shahzad Rana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views14 pages

Pasm

The assembly code extracts the numeric part of a VUID string and stores it in an array. It finds the largest numeric digit and subtracts each digit from the largest, storing the results in a temporary array. Finally, it sorts the array in ascending order.

Uploaded by

Shahzad Rana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

```assembly

section .data

vuid db "VUID-1234567890", 0 ; Assuming VUID is represented as a null-terminated string

numeric_array times 10 db 0 ; Array to store numeric digits of VUID

largest_digit db 0 ; Variable to store the largest numeric digit

updated_vuid times 10 db 0 ; Array to store updated VUID

temp_array times 10 db 0 ; Temporary array to store subtraction results

section .text

global _start

_start:

; Part a) Store the numeric part of VUID in an array

mov esi, vuid ; Load address of VUID string

mov edi, numeric_array ; Load address of numeric_array

call extract_numeric

; Part b) Find the largest numeric digit

mov esi, numeric_array ; Load address of numeric_array

call find_largest_digit

; Part c) Subtract each numeric digit from the largest digit

mov esi, numeric_array ; Load address of numeric_array

mov edi, temp_array ; Load address of temp_array

call subtract_from_largest
; Part d) Sort updated VUID in ascending order

mov esi, temp_array ; Load address of temp_array

mov edi, updated_vuid ; Load address of updated_vuid

call sort_array

; Exit the program

mov eax, 1 ; Syscall number for exit

xor ebx, ebx ; Exit code 0

int 80h ; Invoke syscall

; Function to extract numeric digits from VUID

extract_numeric:

xor ecx, ecx ; Clear counter register

.extract_loop:

mov al, byte [esi + ecx] ; Load character from VUID

cmp al, 0 ; Check for null terminator

je .extract_done ; If null terminator, exit loop

cmp al, '0' ; Compare with ASCII '0'

jl .not_numeric ; If less than '0', not a numeric digit

cmp al, '9' ; Compare with ASCII '9'

jg .not_numeric ; If greater than '9', not a numeric digit

sub al, '0' ; Convert ASCII to numeric value

mov [edi + ecx], al ; Store numeric digit in numeric_array

inc ecx ; Increment counter


jmp .extract_loop

.not_numeric:

inc ecx ; Move to next character

jmp .extract_loop

.extract_done:

ret

; 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

jle .inner_loop ; If not, continue inner loop

inc ecx ; Move to next iteration

cmp ecx, 9 ; Check if all iterations completed

jle .outer_loop ; If not, continue outer loop

ret

```

This assembly code assumes that the VUID is represented as a null-terminated string, and it performs
the required operations as specified in parts (a) to (d) of your requirement. The comments in the code
explain the functionality of each part.

```assembly

section .data

vuid db "VUID-1234567890", 0 ; Assuming VUID is represented as a null-terminated string

numeric_array times 10 db 0 ; Array to store numeric digits of VUID

largest_digit db 0 ; Variable to store the largest numeric digit

updated_vuid times 10 db 0 ; Array to store updated VUID

temp_array times 10 db 0 ; Temporary array to store subtraction results

section .text

global _start
_start:

; Part a) Store the numeric part of VUID in an array

mov esi, vuid ; Load address of VUID string

mov edi, numeric_array ; Load address of numeric_array

call extract_numeric

; Part b) Find the largest numeric digit

mov esi, numeric_array ; Load address of numeric_array

call find_largest_digit

; Part c) Subtract each numeric digit from the largest digit

mov esi, numeric_array ; Load address of numeric_array

mov edi, temp_array ; Load address of temp_array

call subtract_from_largest

; Part d) Sort updated VUID in ascending order

mov esi, temp_array ; Load address of temp_array

mov edi, updated_vuid ; Load address of updated_vuid

call sort_array

; Exit the program

mov eax, 1 ; Syscall number for exit

xor ebx, ebx ; Exit code 0

int 80h ; Invoke syscall


; Function to extract numeric digits from VUID

extract_numeric:

xor ecx, ecx ; Clear counter register

.extract_loop:

mov al, byte [esi + ecx] ; Load character from VUID

cmp al, 0 ; Check for null terminator

je .extract_done ; If null terminator, exit loop

cmp al, '0' ; Compare with ASCII '0'

jl .not_numeric ; If less than '0', not a numeric digit

cmp al, '9' ; Compare with ASCII '9'

jg .not_numeric ; If greater than '9', not a numeric digit

sub al, '0' ; Convert ASCII to numeric value

mov [edi + ecx], al ; Store numeric digit in numeric_array

inc ecx ; Increment counter

jmp .extract_loop

.not_numeric:

inc ecx ; Move to next character

jmp .extract_loop

.extract_done:

ret

; 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

jle .inner_loop ; If not, continue inner loop

inc ecx ; Move to next iteration

cmp ecx, 9 ; Check if all iterations completed

jle .outer_loop ; If not, continue outer loop


ret

```

This assembly code assumes that the VUID is represented as a null-terminated string, and it performs
the required operations as specified in parts (a) to (d) of your requirement. The comments in the code
explain the functionality of each part.

```assembly

section .data

vuid db "VUID-1234567890", 0 ; Assuming VUID is represented as a null-terminated string

numeric_array times 10 db 0 ; Array to store numeric digits of VUID

largest_digit db 0 ; Variable to store the largest numeric digit

updated_vuid times 10 db 0 ; Array to store updated VUID

temp_array times 10 db 0 ; Temporary array to store subtraction results

section .text

global _start

_start:

; Part a) Store the numeric part of VUID in an array

mov esi, vuid ; Load address of VUID string

mov edi, numeric_array ; Load address of numeric_array

call extract_numeric

; Part b) Find the largest numeric digit

mov esi, numeric_array ; Load address of numeric_array

call find_largest_digit
; Part c) Subtract each numeric digit from the largest digit

mov esi, numeric_array ; Load address of numeric_array

mov edi, temp_array ; Load address of temp_array

call subtract_from_largest

; Part d) Sort updated VUID in ascending order

mov esi, temp_array ; Load address of temp_array

mov edi, updated_vuid ; Load address of updated_vuid

call sort_array

; Exit the program

mov eax, 1 ; Syscall number for exit

xor ebx, ebx ; Exit code 0

int 80h ; Invoke syscall

; Function to extract numeric digits from VUID

extract_numeric:

xor ecx, ecx ; Clear counter register

.extract_loop:

mov al, byte [esi + ecx] ; Load character from VUID

cmp al, 0 ; Check for null terminator

je .extract_done ; If null terminator, exit loop

cmp al, '0' ; Compare with ASCII '0'

jl .not_numeric ; If less than '0', not a numeric digit


cmp al, '9' ; Compare with ASCII '9'

jg .not_numeric ; If greater than '9', not a numeric digit

sub al, '0' ; Convert ASCII to numeric value

mov [edi + ecx], al ; Store numeric digit in numeric_array

inc ecx ; Increment counter

jmp .extract_loop

.not_numeric:

inc ecx ; Move to next character

jmp .extract_loop

.extract_done:

ret

; 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

jle .inner_loop ; If not, continue inner loop

inc ecx ; Move to next iteration

cmp ecx, 9 ; Check if all iterations completed

jle .outer_loop ; If not, continue outer loop

ret

```

This assembly code assumes that the VUID is represented as a null-terminated string, and it performs
the required operations as specified in parts (a) to (d) of your requirement. The comments in the code
explain the functionality of each part.

You might also like