MY objective is to collect the digits in a string and print only them. In the end i wish to add them together to return a result, but for now i am stuck at trying to collect them.
I am totaly new to assembly in general (3 hours in) i am a computer science student, we are asked to use NASM assembly with intel syntax and we can't use c library command. I tried to write a code but it turn for some time and then return Timeout:
section .data
message db 'Hell34o, w2or5ld', 0
digits db '0123456789'
section .text
global _start
_start:
; Traverse the string
mov eax, message ; Address of the string
mov ebx, digits ; Address of the digits
mov ecx, 0 ; Index of the next digit occurrence
mov edx, 0 ; Number of digits found
loop:
mov al, byte [eax] ; Load the next character of the string
cmp al, 0 ; Check if it is the end-of-string character
je done ; If yes, exit the loop
mov edi, ebx ; EDI = address of the digits
mov ecx, 0 ; ECX = 0
next_digit:
mov dl, byte [edi+ecx] ; Load the next digit
cmp dl, 0 ; Check if it is the end-of-string character
je skip ; If yes, move to the next digit
cmp al, dl ; Compare the character with the digit
jne next_digit ; If different, try the next digit
; If we are here, then we have found a matching digit
mov byte [eax], dl ; Replace the character with the digit in the string
inc eax ; Move to the next position in the string
inc edx ; Increment the count of digits found
jmp loop ; Go back to the beginning of the loop
skip:
inc ecx ; Move to the next digit in the digit table
jmp next_digit ; Search for the next digit
done:
; Display the found digits
mov eax, 4 ; Code for the sys_write function
mov ebx, 1 ; File descriptor for stdout
mov ecx, message ; Address of the string (now with only digits)
mov edx, edx ; Number of digits found
int 0x80 ; System call to display the string
; End the program
mov eax, 1 ; Code for the sys_exit function
xor ebx, ebx ; Return code 0
int 0x80 ; System call to end the program
I am developing on Linux, if someone could point me toward a solution it would be appreciated i find assembly challenging.