Author Topic: converting string to int problems  (Read 9141 times)

nobody

  • Guest
converting string to int problems
« on: October 18, 2007, 07:03:41 PM »
I need to input a string, convert it to an int, multiply it and convert it back to string then print the new string to screen.

I think i am getting the string input and converting to int correctly. It is multiplying fine i think as well. My problem is converting it back to a string. Something is going wrong. Here is my code at the moment. And sample inputs and outputs that i get at end:

; Progam 2: input/output ints
; Author: William Pognonec

section .bss
our_int resb 30
new_int resb 30
int_len resd 1
newint_len resd 1

section .data
welcome_msg db "Welcome to program 2.",13,10
len1 equ $ - welcome_msg
prompt_int db "Please enter an integer."
len2 equ $ - prompt_int

section .text

global _start

_start:
  ;write welcome message
  mov edx, len1
  mov ecx, welcome_msg
  mov ebx, 1
  mov eax, 4
  int 0x80

;write prompt for int
  mov edx, len2
  mov ecx, prompt_int
  mov ebx, 1
  mov eax, 4
  int 0x80

;now read int
  mov edx, 30
  mov ecx, our_int
  mov ebx, 2
  mov eax, 3
  int 0x80

;now save size of string input
  dec eax
  mov dword [int_len], eax

;now convert number
  mov edi, 10
  mov ecx, [int_len]
  mov esi, our_int
  xor eax, eax
  xor ebx, ebx

mov bl, [esi]
  cmp bl, '-'
  jne next_check
  inc eax
  inc esi
  dec ecx
  jmp done_sign
next_check: cmp bl, '+'
  jne done_sign
  inc esi
  dec ecx
done_sign: push eax

l1: mov bl, [esi]
;  cmp bl, 30h
;  jb error
;  cmp bl, 39h
;  ja error
  sub bl, 30h
  mul edi; eax = eax * 10
  mov bh, 0
  add eax, ebx
  inc esi
  loop l1

pop ebx
  cmp ebx, 1
  jne done
  neg eax

done: ;now tripple eax
  mov edi, 3
  mul edi

;convert int to string
  mov esi, new_int
  mov ebx, 10
  xor ecx, ecx
  cmp eax, 0
  jge skip_negate
  neg eax
  push dword 1; remember neg
  jmp start_convert
skip_negate: push dword 0
start_convert: xor edx, edx
  div ebx
  add dl, 30h; convert ascii
  push edx
  inc ecx
  cmp eax, 0
  jne start_convert
  mov eax, ecx
  inc eax
  inc esi
lp2: pop edx
  mov [esi], dl
  inc esi
  loop lp2
  pop edx
  cmp edx, 1
  jne positive
  mov esi, new_int
  mov [esi], byte '-'
  jmp finish
positive: mov esi, new_int
  mov [esi], byte '+'
finish:
mov [newint_len], eax

;print out new int

mov edx, dword [newint_len]
mov ecx, new_int
mov ebx, 1
mov eax, 4
int 0x80

error:
mov ebx, 0
mov eax, 1
int 0x80



;;; Sample outputs ;;;
input:
333
output:
+999

input:
-333
output:
-3999

It seems to work for positive integers.
For negative ones, it gives the correct answer beyond the -3 part.

Any help would be appreciated.
Thanks

You can email me at ubertoast AT gmail DOT com

nobody

  • Guest
Re: converting string to int problems
« Reply #1 on: October 19, 2007, 12:42:05 AM »
Very nice, with one small problem: when you use eax as a "negative indicator"... you then go on to use it as a "result" without clearing it again.

Nice puzzle!

Best,
Frank

nobody

  • Guest
Re: converting string to int problems
« Reply #2 on: October 19, 2007, 02:00:58 PM »
Frank, thank you very much! I can't beleive i didnt see that. Then again i am used to high level language, still getting used to this stuff.

Thanks for the help.