Не могу перевести число в строку символом ASCII на Assember NASM. То есть 123 -> "1","2","3".Число безззнаковое. Пошел вот таким путем
- Сначала передавал в подпрограмму параметр - число которое нужно обработать.
- Далее делил его каждый раз на 1000000, если в частном что-то есть, значить этот разряд записываем в stack. Предварительно в стеке размещаю 0, и сравниваю с ним на тот случай если кто-то введет 00456 вмето 456. Если в stack-е первым идет число не равное нулю - записываем в stack.
- Уменьшаем наш делитель на 10. То есть каждый раз уменьшаем разряд и в остатке окажется есть что в разряде или нет. Например 205 - 200/100 = 2 остаток 5. Если делитель (проверка разряда) равно 1, то мы просто записываем остаток в Stack и переходи к чтению из stack-a.
- Достаю все из стека, при это ставниваю что вершина stack-a не была ebp, то есть не дошел ли я до конца при это каждый раз инкрементуруя ecx чтобы потом знать длину строки. Такой подход избавляет меня от необходимости вести счет сколько чисел я занес в stack.
- Копирую строку меняя при этом направление записи. Так как 123 в stack запишеться как 123, из стека я достану 321 то есть чтение начинаю сначала изменяю добавляя 48, и записываю с конца.
Уже все тыщу раз пересмотрел, вроде все обмозговал, но не работаем код. Я бы не стал задавать вопрос, так как я только учусь и не умею пользоваться отладчиками. Заранее огромное спасибо всем кто откликнеться. Не хочу бросать, вопрос не решенным, как это часто бывает.
Вот сам код:
global start_create
section .bss
string resb 7 ;variable parameter
string2 resb 7
section .data
string2_end equ string2
string2_length dd 0
division10 dd 10 ;we will add the counter to get a end of string: how many elements we will have after loop
section .text
start_create: push ebp ;save the ebp
mov ebp, esp ;get the link to esp. ebp stores just lik of its value
mov dword [ebp-4], 0 ;set initial value to check if stack has any value
xor edx, edx ;edx:eax/ecx ; set 0 in edx because edx:eax will be the divisible
mov eax, [ebp+8] ;get the number(parameter) transfered by the calling procedure
mov ecx, 1000000 ;set the digit to divide - divisor - YOU LATER CAN SET ANY NUMBER
division: div ecx ;get the quotient of the number
test eax, eax ;check if the quotient zero
jnz put_not_zero ;if it is not so - push the value in stack - otherwise check the first value in stack
cmp dword [ebp-4], 0 ;check if initial value on stack is 0, it means we will not put the number in stack
jnz push_in_stack ;if first argument is not 0, push 0 in stack 0!+0 12...+0
jmp next_digit ;if the first argument is 0
put_not_zero: cmp dword [ebp-4], 0 ;
jnz push_in_stack
mov [ebp-4], eax ;clear the first initial value with normal value
push_in_stack: push eax ;save the digit in stack
next_digit: cmp ecx, 1 ; check if divisor is 10 to finish the loop
jne continue_division
push edx ;if divisor is 10 we will not continue loop. We will put the remainder in stack
jmp read_to_create_string
continue_division:
mov ebx, edx ;move the remainder to ebx to save it
mov eax, ecx ;decrement divisor by 10 lower
xor edx, edx ;zero the value of the edx, because edx is part of division operation
div dword [division10]
mov ecx, eax ;set the new digit divisor
mov eax, ebx ;get the remainder for division operation
jmp division
read_to_create_string:
mov ecx, 0
read_from_stack:
cmp esp, ebp ;check if we go to a start of the adress of procedure [return adress - 4] = ebp, or we do not have string
je create_ascii_symbols
pop edx
mov [string + ecx], dl ;write the in string
inc ecx ;increment ecx to write next number from stack to memory
jmp read_from_stack
create_ascii_symbols:
mov [string2_length], ecx ; change value to strore length of taken string to display it later
add [string2_end], cl ;string2_end == string2 we add cl instead because we add byte to byte, and in ecx we have not so big number
mov esi, string
mov edi, string2_end
add48_to_ASCII: cld ;we do not set value in ecx - counter because we have it afer previously
lodsb ;load number to create ascii symbol
add eax, 48 ;
std ;change direction to write not 3,2,1 to 1,2,3 . To write from the end
stosb ;
loop add48_to_ASCII ;repeat the action several times pointed in ecx - length of the string
;in string we have string of numbers in ASCII
display: mov eax, 4
mov ebx, 1
mov ecx, string
mov edx, string2_length
int 80h
mov esp, ebp
pop ebp
ret