assembly - Why Masm32 only give 1 to 100 result for add and subtraction operation and beyond that I got wrong answer? -


i new assembly language. make code , run no errors far, except give result 1 - 100, here's code.

this simple math operation addition , subtraction. tried analyzing code maybe in num1 db 10 dup (?) tried changing 10 100 still wont work. did miss something?

.386 .model flat, stdcall option casemap :none include \masm32\include\masm32rt.inc  .data     msgec db "choose operation", 00ah, 0     msgadd db "addition 1", 00ah, 0     msgsub db "subtraction 2", 00ah, 0     msgex db "exit 3", 00ah, 0     msg db "addition", 00ah, 0     msgsub2 db "subtraction", 00ah, 0     msg1 db "enter 1st number:", 00ah, 0     msg2 db "enter 2nd number:", 00ah, 0     msg3 db "sum is:", 00ah, 0     msgdf db "diff is:", 00ah, 0     endl db 00ah, 0     msg4 db "try again[1/0]:", 0 .data?      num1 db 10 dup (?)     num2 db 10 dup (?)     res sdword ?     choice db 10 dup (?)     choice2 db 10 dup (?)  .code start:     invoke stdout, addr msgadd     invoke stdout, addr msgsub      invoke stdout, addr msgex     invoke stdout, addr msgec     invoke stdin, addr choice2, 10     mov[choice2 + eax-3], 0     invoke striplf, addr choice2     invoke atodw, addr choice2      .if eax == 1     jmp _addition     .elseif eax == 2     jmp _subtraction     .elseif eax == 3 || eax > 3     jmp _exit     .endif   _addition:      invoke clearscreen     invoke stdout, addr msg     invoke stdout, addr msg1     invoke stdin, addr num1, 10     mov[num1 + eax-3], 0     invoke striplf, addr num1     invoke atodw, addr num1     mov ebx, eax      invoke stdout, addr msg2     invoke stdin, addr num2, 10     mov[num2 + eax-3], 0     invoke striplf, addr num2     invoke atodw, addr num2     add ebx, eax         invoke dwtoa, ebx, addr res     invoke stdout, addr msg3     invoke stdout, addr res     invoke stdout, addr endl      invoke stdout, addr msg4     invoke stdin, addr choice, 10     mov[choice + eax-3], 0     invoke striplf, addr choice     invoke atodw, addr choice      .if eax == 1     jmp _addition     .else     .endif  _subtraction:      invoke clearscreen     invoke stdout, addr msgsub2     invoke stdout, addr msg1     invoke stdin, addr num1, 100     mov[num1 + eax-3], 0     invoke striplf, addr num1     invoke atodw, addr num1     mov ebx, eax      invoke stdout, addr msg2     invoke stdin, addr num2, 100     mov[num2 + eax-3], 0     invoke striplf, addr num2     invoke atodw, addr num2     sub ebx, eax         invoke dwtoa, ebx, addr res     invoke stdout, addr msgdf     invoke stdout, addr res     invoke stdout, addr endl      invoke stdout, addr msg4     invoke stdin, addr choice, 10     mov[choice + eax-3], 0     invoke striplf, addr choice     invoke atodw, addr choice      .if eax == 1     jmp _subtraction     .else      .endif  _exit:  invoke exitprocess, 0  end start 

edit: example out put if add 100 + 5 shows wrong result 5 if add lower number 90 + 5 output right result 95.

all of these operations in code incorrect:

mov[num1 + eax-3], 0 

stdin returns number of characters read, excluding nul terminator. if entered 100, eax 3, you'll set first byte 0, thereby making num1 empty string. worse, if entered 1 digit or 2 digit number you'll writing outside of num1 because eax-3 negative.

fortunately stdin both strip crlf , nul-terminate string you. can remove of mov[foo + eax-3], 0 instructions, invoke striplf, addr foo calls.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -