0

I need to build a calculator program in x64 assembly, where the user inputs two numbers and then the program asks for an operator (+,-,*,/) from the user. I am trying to compare the input with stored variables so I can compare the input with those variables and perform the user specified operation.

When running the program in gdb, I see that if I print/c for operator and variable: addition (when I set operator to +), they both store the same value but in cmp it does not jump to the appropriate block of code

My code is sort of like this

segment .data
  mult db "*"
  divide db "/"
  addition db "+"
  subtract db "-"

  operator db ""

segment .data 
   global _start

_start: 
  ;;get user input, set operator from rsi using syscall
  mov r13,[operator]  
  mov r12,[addition]

  cmp r12,r13 ;; my problem is somewhere here 
  je addFunction ;;Jump to addition operation
  mov r12,[subtract]
  cmp r12,r13
  je subFunction
phuclv
  • 37,963
  • 15
  • 156
  • 475
Jaynill Gopal
  • 47
  • 1
  • 9
  • 1
    You are loading and comparing 64 bit registers instead of 8 bits that a byte is. Use `r12b` and `r13b`. – Jester Aug 29 '18 at 20:06
  • That worked! Thank you – Jaynill Gopal Aug 29 '18 at 20:14
  • well, already `mov r12,[subtract]` is reading 8 bytes of memory, while you are probably interested only into single byte defined by `db "-"` line... etc... Also line `operator db ""` makes me wonder, how that one compiles... does it produce no machine code, just defines label? If you want to use area in `.data` segment for storage of values, you should "reserve" enough space there by defining something, like `operator db "01234567"`, then you can overwrite that with some actual input value. The label itself doesn't reserve anything, nor does it dynamically "inflate" at runtime. – Ped7g Aug 29 '18 at 22:15
  • any particular reason, why you don't do `mov r13b,[operator]` and then `cmp r13b,'+'` without the "addition" memory clutter? – Ped7g Aug 29 '18 at 22:17

0 Answers0