0

I'm trying to read from the keyboard some integers, add them and calculate the result. For some reason it's skipping the scanf when I'm trying to read the operation, so I can't go further with my program.

The part in the code below where this is called is:

push offset opr_adr 
push offset opr 
call scanf 
add ESP,8

The code is:

.386
.model flat, stdcall
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;libraries and functions
includelib msvcrt.lib
extern printf: proc
extern scanf: proc
extern exit: proc
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;declare start symbol as public
public start
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;sectiunile programului, date, respectiv cod
.data
msg1 db "Base(decimal or hexadecimal): ", 0
msg2 db "Expression: ", 0
msg3 db "Expression(hexadecimal): ", 0

base_adr Db 2 dup(0)     ;base address
baza db "%c",0           ;base, D for decimal, H for hexadecimal

opr_adr Db 4 dup(0),0    ;operator address
opr db "%c",0

nr db 4 dup(0)
number Db "%d",0

result db "%d",0    
rez db 0



.code


start:
push offset msg1
call printf
add ESP, 4

push offset base_adr
push offset baza
call scanf
add ESP, 8

cmp base_adr,'D'
je decimal
cmp base_adr,'H'
jz hexadecimal

decimal:
push offset msg2
call printf
add ESP, 4
jmp decimal_numbers

decimal_numbers:
push offset nr
push offset number
call scanf
mov bx, 0
mov bl,nr
add ESP,8

push offset opr_adr
push offset opr
call scanf
add ESP,8

cmp opr_adr, '+' 
je adunare
cmp opr_adr, '=' 
je result


adunare:
add Al, Bl
jmp decimal_numbers


result:
mov rez, AH
push offset rez
push offset rezultat
call printf
add esp,8


hexadecimal:
push offset msg3
call printf
add ESP,4


;terminarea programului
push 0
call exit
end start
Tony
  • 1,645
  • 1
  • 10
  • 13
sixfeet
  • 934
  • 4
  • 16
  • 33
  • 1
    Have you stepped through your code with a debugger? Or are you following the compile, execute and post-to-Stack-Overflow-when-it-doesn't-work-perfectly development model? – Jonathon Reinhart Dec 13 '14 at 21:48
  • I used Olly Debugger, but it doesn't stop to the scanf so I can give the operator, and I don't understand why... – sixfeet Dec 13 '14 at 21:51
  • How are you building this program? I see that your symbol is called `start` - are you not linking against the libc startup files? – Jonathon Reinhart Dec 13 '14 at 21:53
  • I've used this symbol in other programs too and it was working fine... – sixfeet Dec 13 '14 at 22:00
  • Well what does `scanf` return? Is your variable getting set appropriately? Perhaps there's already data in the input buffer - you could try [adding a space to the front of your format string](http://stackoverflow.com/q/6582322/119527). – Jonathon Reinhart Dec 13 '14 at 22:03
  • Great, that worked. Can you please explain to me what is the logic behind this? Why is **" %c"** working and **"%c** isn't? – sixfeet Dec 13 '14 at 22:14
  • 1
    http://stackoverflow.com/a/6582378/119527 – Jonathon Reinhart Dec 13 '14 at 22:17

0 Answers0