flat assembler
Message board for the users of flat assembler.
![]() Goto page Previous 1, 2, 3 Next |
Author |
|
FlierMate
Code: fastcall [scanf],'%d',operand1 fastcall [scanf],'%d',operand2 mov rcx, [operand1] add [operand2], rcx fastcall [printf],'%d',[operand2] This works for me.... ![]() |
|||
![]() |
|
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? |
|||
![]() |
|
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. |
|||
![]() |
|
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 |
|||
![]() |
|
FlierMate
retro wrote: > If I change from rcx to rax, this actually works as well: The macroinstruction overrides the rcx? Code: macro fastcall proc,[arg]..... Though someone more knowledgeable should be able to give a better explanation. ![]() Last edited by FlierMate on 17 Apr 2022, 16:43; edited 1 time in total |
|||
![]() |
|
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? |
|||
![]() |
|
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. |
|||
![]() |
|
FlierMate
I am not sure if you can make use of _kbhit
|
|||
![]() |
|
retro
maybe i can, i think i can also use _getch.
|
|||
![]() |
|
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. |
|||
![]() |
|
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... |
|||
![]() |
|
macomics
Code: mov rax, [operand1] cdqe ; rdx = sign(rax) i.e. 0 or -1 idiv [operand2] mov [answer], rax mov [remains], rdx |
|||
![]() |
|
retro
well, it didn't work for me, the program still hangs and crashes after typing in 2 numbers at the division part...
|
|||
![]() |
|
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.
|
|||
![]() |
|
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]". |
|||
![]() |
|
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 |
|||
![]() |
|
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 |
|||
![]() |
|
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 |
|||
![]() |
|
FlierMate
macomics wrote: I'm always confused which one is 32-bit. 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. |
|||
![]() |
|
Goto page Previous 1, 2, 3 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.
Website powered by rwasa.