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
Thread Post new topic Reply to topic
retro



Joined: 12 Oct 2021
Posts: 47
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:


Description:
Download
Filename: zcalc.asm
Filesize: 1.99 KB
Downloaded: 245 Time(s)

Post 17 Apr 2022, 04:16
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 17 Apr 2022, 07:18
Try adding "and rsp, -16" after "start:"
Code:
start:
    and rsp, -16
    ccall [printf], welcome    
Also you need to remove the brackets for operand1
Code:
        ccall [gets], operand1    
Post 17 Apr 2022, 07:18
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 926
Location: Russia
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    
Post 17 Apr 2022, 08:52
View user's profile Send private message Reply with quote
retro



Joined: 12 Oct 2021
Posts: 47
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...
Post 17 Apr 2022, 11:25
View user's profile Send private message Reply with quote
retro



Joined: 12 Oct 2021
Posts: 47
retro 17 Apr 2022, 11:26
maybe i was using scanf wrong this whole time.
Post 17 Apr 2022, 11:26
View user's profile Send private message Reply with quote
retro



Joined: 12 Oct 2021
Posts: 47
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...
Post 17 Apr 2022, 11:35
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4020
Location: vpcmpistri
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'    
... or were you trying to make an integer calculator?

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
Post 17 Apr 2022, 13:45
View user's profile Send private message Visit poster's website Reply with quote
retro



Joined: 12 Oct 2021
Posts: 47
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.
Post 17 Apr 2022, 13:48
View user's profile Send private message Reply with quote
retro



Joined: 12 Oct 2021
Posts: 47
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.
Post 17 Apr 2022, 14:19
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 926
Location: Russia
macomics 17 Apr 2022, 14:27
Code:
16: fastcall [scanf],'%lf',myDouble; without ADDR
; or
16: fastcall [scanf],'%lf',ADDR [myDouble]    
Post 17 Apr 2022, 14:27
View user's profile Send private message Reply with quote
retro



Joined: 12 Oct 2021
Posts: 47
retro 17 Apr 2022, 14:44
oh.
Post 17 Apr 2022, 14:44
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 219
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.
Post 17 Apr 2022, 14:56
View user's profile Send private message Reply with quote
retro



Joined: 12 Oct 2021
Posts: 47
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...
Post 17 Apr 2022, 15:06
View user's profile Send private message Reply with quote
retro



Joined: 12 Oct 2021
Posts: 47
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
Post 17 Apr 2022, 15:25
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 926
Location: Russia
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    
This is a sample code. Based on how I remember macros working. Did not specify.


Last edited by macomics on 17 Apr 2022, 15:35; edited 1 time in total
Post 17 Apr 2022, 15:25
View user's profile Send private message Reply with quote
retro



Joined: 12 Oct 2021
Posts: 47
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?
Post 17 Apr 2022, 15:32
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 926
Location: Russia
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    
As one of the options.

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
Post 17 Apr 2022, 15:37
View user's profile Send private message Reply with quote
retro



Joined: 12 Oct 2021
Posts: 47
retro 17 Apr 2022, 15:42
oh.
Post 17 Apr 2022, 15:42
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 926
Location: Russia
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
Post 17 Apr 2022, 15:58
View user's profile Send private message Reply with quote
retro



Joined: 12 Oct 2021
Posts: 47
retro 17 Apr 2022, 16:04
thanks for the explanation, i think i should read more about debuggers now...
Post 17 Apr 2022, 16:04
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2, 3  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.