flat assembler
Message board for the users of flat assembler.

Index > Main > [solved] program hangs for a second and crashes

Goto page Previous  1, 2, 3  Next
Author
Thread Post new topic Reply to topic
FlierMate



Joined: 21 Jan 2021
Posts: 208
FlierMate
Code:
        fastcall [scanf],'%d',operand1
        fastcall [scanf],'%d',operand2
        mov rcx, [operand1]
        add [operand2], rcx
        fastcall [printf],'%d',[operand2]   
    


This works for me.... Surprised
Post 17 Apr 2022, 16:18
View user's profile Send private message Visit poster's website Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 208
FlierMate
If I change from rcx to rax, this actually works as well:

Code:
    addition:
        fastcall [printf], add_
        fastcall [scanf],'%d',operand1
        fastcall [scanf],'%d',operand2
        mov rax, [operand1]
        add rax, [operand2]
        fastcall [printf],'%d', rax     
    
Post 17 Apr 2022, 16:31
View user's profile Send private message Visit poster's website Reply with quote
retro



Joined: 12 Oct 2021
Posts: 26
retro
ah, now the calculator itself is working!
note that i said the calculator itself, not the whole program...
now i have to fix that problem where after the user presses any key to continue the program, the program simply exits.
i think what's happening is that the program is ignoring the getchar at the beginning, but why's it doing that?
Post 17 Apr 2022, 16:32
View user's profile Send private message Reply with quote
retro



Joined: 12 Oct 2021
Posts: 26
retro
> If I change from rcx to rax, this actually works as well:
is there a "best" register to use in this case? or it doesn't matter at all and i can use any register for this calculator? i'm guessing in an integer calculator like mine, it shouldn't matter what register it's using, but in float/ double calculators it would matter a lot.
Post 17 Apr 2022, 16:35
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 462
Location: Russia
macomics
scanf does not clear the input characters before returning. i.e. "\n" remains in the buffer. And you react to this by exiting the program. Replace Enter with any letter key and you will see. For integer calculations, you can use any free general-purpose register (i.e. except rsp)


Last edited by macomics on 17 Apr 2022, 16:40; edited 1 time in total
Post 17 Apr 2022, 16:37
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 208
FlierMate
retro wrote:
> If I change from rcx to rax, this actually works as well:
is there a "best" register to use in this case? or it doesn't matter at all and i can use any register for this calculator? i'm guessing in an integer calculator like mine, it shouldn't matter what register it's using, but in float/ double calculators it would matter a lot.


The macroinstruction overrides the rcx?
Code:
macro fastcall proc,[arg].....    


Though someone more knowledgeable should be able to give a better explanation. Embarassed


Last edited by FlierMate on 17 Apr 2022, 16:43; edited 1 time in total
Post 17 Apr 2022, 16:38
View user's profile Send private message Visit poster's website Reply with quote
retro



Joined: 12 Oct 2021
Posts: 26
retro
> scanf does not clear the input characters before returning. i.e. "\n" remains in the buffer. And you react to this by exiting the program. Replace Enter with any letter key and you will see.
i was guessing that right now, apparently i was right.
so, do i have to xor al, al at the beginning of the program so the register gets cleared and then the user can go back to the menu and perform what they want?
Post 17 Apr 2022, 16:41
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 462
Location: Russia
macomics
Both FPU and SSE can be used for floating-point calculations.

retro wrote:
> scanf does not clear the input characters before returning. i.e. "\n" remains in the buffer. And you react to this by exiting the program. Replace Enter with any letter key and you will see.
i was guessing that right now, apparently i was right.
so, do i have to xor al, al at the beginning of the program so the register gets cleared and then the user can go back to the menu and perform what they want?
Strangely, it has no arguments in the getchar description.
Post 17 Apr 2022, 16:43
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 208
FlierMate
I am not sure if you can make use of _kbhit
Post 17 Apr 2022, 16:50
View user's profile Send private message Visit poster's website Reply with quote
retro



Joined: 12 Oct 2021
Posts: 26
retro
maybe i can, i think i can also use _getch.
Post 17 Apr 2022, 16:51
View user's profile Send private message Reply with quote
retro



Joined: 12 Oct 2021
Posts: 26
retro
there, the calculator is almost completed, the only problem is in the division part:
after the user types in the 2 numbers, the program hangs for a second and crashes.
i think i'll search for a division code on the internet and see if i can adapt it to my design.
Post 17 Apr 2022, 17:08
View user's profile Send private message Reply with quote
retro



Joined: 12 Oct 2021
Posts: 26
retro
couldn't find on the internet, so i decided to check on the debugger to see what's wrong with it:
"40128D: Integer overflow (exc.code c0000095, tid 11028)".
hmm...
Post 17 Apr 2022, 17:21
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 462
Location: Russia
macomics
Code:
mov rax, [operand1]
cdqe ; rdx = sign(rax) i.e. 0 or -1
idiv [operand2]
mov [answer], rax
mov [remains], rdx    
What's the problem?
Post 17 Apr 2022, 17:21
View user's profile Send private message Reply with quote
retro



Joined: 12 Oct 2021
Posts: 26
retro
well, it didn't work for me, the program still hangs and crashes after typing in 2 numbers at the division part...
Post 17 Apr 2022, 17:37
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 462
Location: Russia
macomics
Then you can give the code that you wrote and where exactly you have a problem. Fortune-telling on coffee grounds is not the best way to help.
Post 17 Apr 2022, 17:41
View user's profile Send private message Reply with quote
retro



Joined: 12 Oct 2021
Posts: 26
retro
Code:
division:
        xor rax, rax
        xor rcx, rcx
        fastcall [printf], div_
        fastcall [scanf],'%d',operand1
        fastcall [scanf],'%d',operand2
        mov rax, [operand1]
        cdqe ; rdx = sign(rax) i.e. 0 or -1
        idiv [operand2]
        fastcall [printf],'%d', rax
        fastcall [printf],'%d', rdx
        call [getch]
        jmp start    

and apparently, from what i saw in the debugger, the problem is in "idiv [operand2]".
Post 17 Apr 2022, 17:51
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 462
Location: Russia
macomics
Code:
  mov rcx, [operand2]
  mov rax, [operand1]
  cdqe
  jrcxz div_by_zero ; div-by-0
  idiv rcx
; fastcall [printf],'%d', rax
  sub rsp, 32
  mov rdx, rax ; remains - disappear
  jmp @f
 .str0 db "%d", NULL
 @@:
  lea rcx, [.str0]
  call [printf]
  add rsp, 32
; fastcall [printf],'%d', rdx
  sub rsp, 32
; mov rdx, rdx <- unused
  jmp @f
 .str1 db "%d", NULL
 @@:
  lea rcx, [.str1]
  call [printf]
  add rsp, 32
...
div_by_zero:
  fastcall [printf], "Div by 0!"    


Last edited by macomics on 17 Apr 2022, 18:38; edited 1 time in total
Post 17 Apr 2022, 18:18
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 208
FlierMate
I have a shorter version: (I am not sure why CDQ worked but not CDQE)

Code:
    division:
        xor rax, rax
        ;xor rcx, rcx
        fastcall [printf], div_
        fastcall [scanf],'%d',operand1
        fastcall [scanf],'%d',operand2
        mov rax, [operand1]
        cdq ; rdx = sign(rax) i.e. 0 or -1
        idiv [operand2]
        fastcall [printf],'%d', rax 
    
Post 17 Apr 2022, 18:25
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 462
Location: Russia
macomics
I'm always confused which one is 32-bit.
Code:
CDQE ; rax = uuuu`uuuu`svvv`vvvv; rdx = 0000`0000`yyyy`yyyy
CDQ  ; rax = svvv`vvvv`vvvv`vvvv; rdx = yyyy`yyyy`yyyy`yyyy
; u - undef hex-digit
; s - sign hex-digit
; y - sign bits (0 or F)
; v - value hex-digit    
Post 17 Apr 2022, 18:30
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 208
FlierMate
macomics wrote:
I'm always confused which one is 32-bit.
Code:
CDQE ; rax = uuuu`uuuu`svvv`vvvv; rdx = 0000`0000`yyyy`yyyy
CDQ  ; rax = svvv`vvvv`vvvv`vvvv; rdx = yyyy`yyyy`yyyy`yyyy
; u - undef hex-digit
; s - sign hex-digit
; y - sign bits (0 or F)
; v - value hex-digit    


I see, both is:convert doubleword to quadword

cdq
Code:
%eax -> %edx:%eax    


cdqe
Code:
%eax -> %rax    


This is my first time encounter with CDQE. Good to learn from you.
Post 17 Apr 2022, 18:41
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.