Search code examples
arraysassemblyindexingscanfmasm

Assembly sscanf


I am trying to call sscanf on a string that would look something like "2.3 + 5.89" in assembly. I want to store the values in the array called 'values' and the operators from the string in the array called 'op'. After getting the first value i looped through the string to find the next space and find the position of the operator. My question is: how can i get the operators in the array 'op' using sscanf? What would i give the offest of the string 'exp' + the position of the operator? Or is there an easier approach for this?

lea edi, dword ptr values 
push dword ptr edi
push offset format
push offset exp
call sscanf
add esp, 12

mov esi,0

for_i:
inc i
mov bl,i 
inc esi
cmp exp[esi],' '
jne for_i
inc i

lea esi, dword ptr op 
push dword ptr esi
push offset format_op
push offset exp + i **--- here is my bug** 
call sscanf
add esp, 12 

Solution

  • I think you want to pass sscanf the address of an array element. That means you need to do exp + runtime_variable, not adding two symbol addresses into an assemble-time constant. Remember that every line of asm has to be something the machine can do in one instruction.

    First of all, keep your loop counter in a register like ebx; don't use a static variable called i at all. (EBX, ESI, EDI, EBP aren't destroyed by function calls so you can use any of them in asm the way you'd use a local variable in C. But of course your own caller expects their values of those regs to be preserved so do that around your function.)

    Then lea esi, [exp + esi*4] / push esi will compute the address and push it.