flat assembler
Message board for the users of flat assembler.
Index
> Main > [solved] program hangs for a second and crashes Goto page 1, 2, 3 Next |
Author |
|
retro 17 Apr 2022, 04:16
hi, i'm trying to make a simple calculator in x64 assembly, however, i'm having the following issue:
the program hangs for a second and then crashes. i have tried: changing the way the c functions for reading and writing data (gets and printf) are used: got the same results... adding those brackets to the variables (who knows, 'cos i dunno...): the program crashes right before the user types in a number... according to my debugger (ida freeware), this is what happens: "7FFFAD89F5C6: The instruction at 0x7FFFAD89F5C6 referenced memory at 0x10040210B. The memory could not be read -> 000000010040210B (exc.code c0000005, tid 4264)". i think what it's trying to say is that the variable couldn't be written to the register 'cos the system can't read the variable, as you can see in the code, it (tries to) write the value in the variable to the register so the operation can be performed:
|
|||||||||||
17 Apr 2022, 04:16 |
|
macomics 17 Apr 2022, 08:52
You're passing a pointer to a general-purpose register, are you?
Code: ccall [getchar], al Note that to enter a string via gets, you have an 8-byte buffer (5-character with ending "\n"+NULL). Maybe it's better this way. Are you going to add and subtract strings? Code: ccall [scanf], format_number, operand1 ... format_number db "%d", 0 |
|||
17 Apr 2022, 08:52 |
|
retro 17 Apr 2022, 11:25
oh right, i didn't use scanf 'cos for some unknown reason it always makes my programs crash, so i use gets instead...
|
|||
17 Apr 2022, 11:25 |
|
retro 17 Apr 2022, 11:26
maybe i was using scanf wrong this whole time.
|
|||
17 Apr 2022, 11:26 |
|
retro 17 Apr 2022, 11:35
and now i tried what you guys suggested, but it didn't work at all, i still got the same results...
|
|||
17 Apr 2022, 11:35 |
|
bitRAKE 17 Apr 2022, 13:45
Does this work for you?
Code: format PE64 console entry start include 'win64a.inc' section '.text' code readable executable start: push rax if 0 fastcall [scanf],'%f',ADDR myFloat cvtss2sd xmm0,[myFloat] movq [myDouble],xmm0 fastcall [printf],'myFloat: %f',[myDouble] else fastcall [scanf],'%lf',ADDR myDouble fastcall [printf],'myDouble: %lf',[myDouble] end if fastcall [exit],0 section '.data' data readable writeable myDouble dq ? myFloat dd ? section '.idata' import data readable library msvcrt,'msvcrt.dll' import msvcrt,\ exit,'exit',\ printf,'printf',\ scanf,'scanf' Everything in 64-bit is fastcall -- they use different names, but it's usually fastcall. Floats are tricky because MS doesn't follow their own calling convention! _________________ ¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup |
|||
17 Apr 2022, 13:45 |
|
retro 17 Apr 2022, 13:48
oh, i was thinking about making an integer calculator and in the future make a double calculator lol, but thanks, i'm gonna check that code later, then.
|
|||
17 Apr 2022, 13:48 |
|
retro 17 Apr 2022, 14:19
well, the code doesn't work at all, fasm says:
"Error: Invalid operand" and it points to line 16. |
|||
17 Apr 2022, 14:19 |
|
macomics 17 Apr 2022, 14:27
Code: 16: fastcall [scanf],'%lf',myDouble; without ADDR ; or 16: fastcall [scanf],'%lf',ADDR [myDouble] |
|||
17 Apr 2022, 14:27 |
|
retro 17 Apr 2022, 14:44
oh.
|
|||
17 Apr 2022, 14:44 |
|
FlierMate 17 Apr 2022, 14:56
Code: ccall [printf], add_ ccall [gets], operand1 ;mov rcx, operand1 ccall [gets], operand2 ;add rcx, operand2 I think conversion from string to numeric value is necessary. |
|||
17 Apr 2022, 14:56 |
|
retro 17 Apr 2022, 15:06
well, now i switched to scanf and i'm getting the same issue, the program crashes after typing in the first number.
let's see what the debugger has to say: "7FFFAD89F5C6: The instruction at 0x7FFFAD89F5C6 referenced memory at 0x10040210F. The memory could not be read -> 000000010040210F (exc.code c0000005, tid 10692)". and, that's just slightly different than before... |
|||
17 Apr 2022, 15:06 |
|
retro 17 Apr 2022, 15:25
now i declared a variable called "format_" with value "%d, 0", i added it to my code, and now i get this:
if i type in 1 at both scanf's at the addition part, i get "4202757" instead of "2", and if i press enter, it goes back to the menu as expected, but, the program exits right away, it doesn't wait for the user input at all... by the way, i think this result (4202757) can be actually an offset, why is it displaying a location instead of the result, though? Last edited by retro on 17 Apr 2022, 15:25; edited 1 time in total |
|||
17 Apr 2022, 15:25 |
|
macomics 17 Apr 2022, 15:25
So hover the cursor over the push rax line in the debugger and put a breakpoint. Why do you use a debugger at all if you ask such questions?
Even if you just estimate an approximate command at the address you have given, it will be "%lf". Code: start: push rax ; ...2000 sub rsp, 20 ; ...2001 lea rdx, [myDouble] ; ...2004 jmp .skip ; ...200A .str db "%lf",0 ; 000000010040200F <--- Error reading from that place .skip: lea rcx, [.str] ; ...2013 call [scanf] ; ...2019 Last edited by macomics on 17 Apr 2022, 15:35; edited 1 time in total |
|||
17 Apr 2022, 15:25 |
|
retro 17 Apr 2022, 15:32
> Even if you just estimate an approximate command at the address you have given, it will be "%lf".
what do you mean by that? |
|||
17 Apr 2022, 15:32 |
|
macomics 17 Apr 2022, 15:37
Code: fastcall [scanf], ADDR [scanf_format_double], ADDR [myDouble] ... myDouble dq ? myFloat dd ? scanf_format_double db '%lf',0 align 16 ADD: Only the previous post is what I approximately typed in the browser. Initially, I suggested that you find the exact command that causes the error by means of the debugger. If you find it, it will be possible to talk about correcting the error. Last edited by macomics on 17 Apr 2022, 15:42; edited 1 time in total |
|||
17 Apr 2022, 15:37 |
|
retro 17 Apr 2022, 15:42
oh.
|
|||
17 Apr 2022, 15:42 |
|
macomics 17 Apr 2022, 15:58
Any debugger has breakpoints, they allow you to stop the execution of the program when a certain command is reached. Then you can use the Step Over and Step In functions. The first one executes command without executing the call/int (they set a breakpoint after the call/int and wait for it to be reached). The second one jumps to the called address and allows you to debug the function cmd-by-cmd. Of course, it makes no sense to debug function calls from msvcrt (Step Over), and your own function should be passed cmd-by-cmd (Step In).
Last edited by macomics on 17 Apr 2022, 17:17; edited 1 time in total |
|||
17 Apr 2022, 15:58 |
|
retro 17 Apr 2022, 16:04
thanks for the explanation, i think i should read more about debuggers now...
|
|||
17 Apr 2022, 16:04 |
|
Goto page 1, 2, 3 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.