PS2=64 - What instruction is this?

Bare metal programming in PureBasic, for experienced users
ricardo_sdl
Enthusiast
Enthusiast
Posts: 109
Joined: Sat Sep 21, 2019 4:24 pm

PS2=64 - What instruction is this?

Post by ricardo_sdl »

I looked at the assembly for a simple procedure:

Code: Select all

; Procedure.i IsOdd(n.i)
_Procedure2:
  MOV    qword [rsp+8],rcx
  PUSH   r15
  PS2=64
  XOR    rax,rax
  PUSH   rax
  SUB    rsp,40
; ProcedureReturn n & %1
  MOV    r15,qword [rsp+PS2+0]
  AND    r15,1
  MOV    rax,r15
  JMP   _EndProcedure3
; EndProcedure
_EndProcedureZero3:
  XOR    rax,rax
_EndProcedure3:
  ADD    rsp,48
  POP    r15
  RET
What is the instruction PS2=64? PS2 doesn't appear anywhere else in the assembly code. Is it something peculiar to the assembler (flatassembler)? My PureBasic version is PureBasic 5.73 LTS (Windows - x64).
You can check my games at:
https://ricardo-sdl.itch.io/
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: PS2=64 - What instruction is this?

Post by STARGÅTE »

It is an offset for the stack and it is used in
MOV r15,qword [rsp+PS2+0]
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
ricardo_sdl
Enthusiast
Enthusiast
Posts: 109
Joined: Sat Sep 21, 2019 4:24 pm

Re: PS2=64 - What instruction is this?

Post by ricardo_sdl »

STARGÅTE wrote: Wed Nov 17, 2021 6:18 pm It is an offset for the stack and it is used in
MOV r15,qword [rsp+PS2+0]
In the context of the assembler PS2 is a local variable for the procedure? Shouldn't it be declared in some place? Is PS2 some kind of macro available in flatassembler? Is it idiomatic for x64 assembly language?
You can check my games at:
https://ricardo-sdl.itch.io/
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: PS2=64 - What instruction is this?

Post by STARGÅTE »

ricardo_sdl wrote: Wed Nov 17, 2021 6:57 pm
STARGÅTE wrote: Wed Nov 17, 2021 6:18 pm It is an offset for the stack and it is used in
MOV r15,qword [rsp+PS2+0]
In the context of the assembler PS2 is a local variable for the procedure? Shouldn't it be declared in some place? Is PS2 some kind of macro available in flatassembler? Is it idiomatic for x64 assembly language?
It is a global variable in ASM

Code: Select all

Define x.i

! abc = 123
! MOV [v_x], abc

Debug x
It is named as PS2 because it is used in _Procedure2. In case of another procedure, the number in the name is increasing.
The value itself depends on the number and size of arguments and protected variables in the procedure, which are stored in the stack.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
ricardo_sdl
Enthusiast
Enthusiast
Posts: 109
Joined: Sat Sep 21, 2019 4:24 pm

Re: PS2=64 - What instruction is this?

Post by ricardo_sdl »

Thank you STARGÅTE! I searched a little more and found this:
https://flatassembler.net/docs.php?article=manual#2.2.1

So the value of PS2 is simply stored with the assembled instructions, right?
You can check my games at:
https://ricardo-sdl.itch.io/
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: PS2=64 - What instruction is this?

Post by STARGÅTE »

ricardo_sdl wrote: Wed Nov 17, 2021 8:06 pm So the value of PS2 is simply stored with the assembled instructions, right?
Yes, like a #constant in Pure Basic
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
juergenkulow
Enthusiast
Enthusiast
Posts: 544
Joined: Wed Sep 25, 2019 10:18 am

Re: PS2=64 - What instruction is this?

Post by juergenkulow »

@ricardo_sdl
What debbuger are you using to see what is happening on the stack and in the registers?
Please ask your questions, because switch on the cognition apparatus decides on the only known life in the universe.Wersten :DDüsseldorf NRW Germany Europe Earth Solar System Flake Bubble Orionarm
Milky Way Local_Group Virgo Supercluster Laniakea Universe
ricardo_sdl
Enthusiast
Enthusiast
Posts: 109
Joined: Sat Sep 21, 2019 4:24 pm

Re: PS2=64 - What instruction is this?

Post by ricardo_sdl »

Hi juergenkulow!
In this case I wasn't using the debugger. I looked at the asm output of the compiler with the /commented flag. A tool that used once was https://x64dbg.com/#start, you can look at the stack, memory and registers, really useful to learn assembly.
You can check my games at:
https://ricardo-sdl.itch.io/
Post Reply