Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append converted number to existing string

Tags:

assembly

I have used one simple algorithm for converting number to string found here on SO which I have adjusted for my own purposes, but it doesn't exactly do what I need. I have a string:

string point, "xxx points"

And constant:

const10    dd 10
points     dd 50  ; range from 0 - 990

The adjusted routine

mov eax, [points] ; number to be converted
  .convNum:      
      push eax
      push edx
      xor edx,edx       
      div dword [const10]  ;eax = result, edx = remainder
      cmp eax, dword 0         ; test of the res
      je .print               
      jmp .convNum   
  .print:
      lea eax,[edx+'0']
      mov [point], dword eax ; need to replace xxx in string by number
      pop edx
      pop eax

This one doesn't work as intended, because it shows only one digit, which I understand, but I am not able to change the routine in a way I want.
EDIT:
Adding another version of code

push eax
    push edx
    mov eax, [points]
  .convNum:             
      xor edx,edx       
      div dword [const10]  
      lea eax,[edx+'0']
      mov ecx,[counter]
      cmp ecx, 1
      jne .next
      mov [point+2], byte al
      mov eax,[points]
      div dword [const10]
      jmp .again
      .next:
      cmp ecx, 2
      jne .next2
      mov [point+1], byte al
      mov eax,[points]
      div dword [const10]
      div dword [const10]
      jmp .again
      .next2:
      mov ecx,[p_bodu]
      cmp ecx, 100
      jb .kend
      mov [point], byte al    
      jmp .kend
      .again:
      inc ecx
      mov [counter],ecx
      jmp .convNum
      .kend:
      pop edx
      pop eax

This code takes the number from lowest to highest. Numbers like 120 convert well, but number like 130,150 convert to 630, 650, again number 140 converts well

like image 483
Croolman Avatar asked Apr 06 '26 23:04

Croolman


1 Answers

You keep pushing EAX and EDX in a loop. You only need to do this once.

Move the .convNum label 2 lines lower:

   mov eax, [points] ; number to be converted
   push eax
   push edx
.convNum:      
   xor edx,edx       
   div dword [const10]  ;eax = result, edx = remainder
   cmp eax, dword 0         ; test of the res
   jne .convNum   
.print:

Also note that you should write a byte instead of a dword:

mov [point], al ;

The code you wrote will only process the highest digit of the value provided. To continu retrieving lower placed digits you need to subtract the current digit times (a multiple of) 10 from the number and repeat the code.

like image 154
Fifoernik Avatar answered Apr 09 '26 14:04

Fifoernik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!