I am writing a program to print out all the numbers from 0 to 100, and I need to find the number of digits a variable(in this case the variable counter) holds.
Here is my code:
SECTION .data
len EQU 32
SECTION .bss
counter resd len
digit1 resd len
digit2 resd len
digit3 resd len
SECTION .text
GLOBAL _start
_start:
nop
Print:
mov eax, 4
mov ebx, 1
mov ecx, counter
mov edx, len
int 80h
Set:
mov BYTE [counter], 1
Divide:
; HERE IS WHERE I NEED TO FIND THE LENGTH OF THE VARIABLE COUNTER
; initial division
mov ax, [counter] ; number we want to print
mov ch, 10 ; we divide by ten to siphon digits
div ch ; divide our number by 10
; al now has 11, ah has 1
mov dh, ah ; save the remainder in dh
xor ah,ah
mov ch, 10 ; refill ch with the divisor
div ch ; al now has 1, ah now has 1
Move: ; now to move our digits to a printable state
mov [digit1], dh ; first digit is in edx
mov [digit2], ah
mov [digit3], al
Adjust:
add BYTE [digit1], '0'
add BYTE [digit2], '0'
add BYTE [digit3], '0'
Print:
mov eax, 4
mov ebx, 1
mov ecx, digit1
mov edx, len
int 80h
mov eax, 4
mov ebx, 1
mov ecx, digit2
mov edx, len
int 80h
mov eax, 4
mov ebx, 1
mov ecx, digit3
mov edx, len
int 80h
Exit:
mov eax, 1
mov ebx, 0
int 80h
I need to find the length so that I know how many times to divide and also how many digits to print the variable counter.
How can I find how long it is?
Thanks in advance
For numbers in the range
0..100, I’d just compare at the boundaries, with pseudo-assembler like:That will actually handle up to 999 but you could also add more condition checks before the 100 one if you wanted to expand the range.