Recent

Author Topic: IRC channel  (Read 6452 times)

Joanna

  • Full Member
  • ***
  • Posts: 227
Re: IRC channel
« Reply #45 on: November 29, 2022, 01:20:17 pm »
Quote
Old style objects afaik only get a VMT, if they have virtual methods.

That makes sense.  It’s interesting how function calls take stack space but dynamically allocated variables take heap space maybe because the function calls are considered to be more temporary?  I don’t know much about hardware I’m curious about how heap space is implemented compared to stack space? Is stack space RAM?
Come chat on IRC .. https://libera.chat/guides/
IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channels  #fpc #lazarus #pascal 
type /msg nickserv help register to get started

440bx

  • Hero Member
  • *****
  • Posts: 3265
Re: IRC channel
« Reply #46 on: November 29, 2022, 02:47:36 pm »
Old style objects afaik only get a VMT, if they have virtual methods.
Correct.  I should have mentioned that.



It’s interesting how function calls take stack space but dynamically allocated variables take heap space maybe because the function calls are considered to be more temporary? 
The amount of memory used will be the same whether the object is allocated on the stack or the heap because the size of the VMT and the fields that make up the object are independent of where in memory they reside.

The important difference is that if an object resides on the stack then, when the function/procedure/method where it was declared returns then the object "disappears" because the stack pointer is reset to whereever it was before that function/procedure/method was invoked.  If the object resides in the heap then returning from the function/procedure/method has no effect on the existence of the object because heap allocations are not affected by the value  of the stack pointer.


I don’t know much about hardware I’m curious about how heap space is implemented compared to stack space? Is stack space RAM?
Both, heap and stack are plain, standard RAM.  The difference between the two is how they are _managed_.  They are managed _very_ differently.

if you want to learn how things really work then learning a little bit of Assembly language is the way to go.  A set of well known and good tutorials are those of Iczelion, google "iczelion tutorials", note: his tutorials are a bit all over the place on the net because it looks like he no longer hosts them himself.  Learning at least the basics of Assembly language programming will significantly enhance your programming skills and knowledge.  There is a lot to learn but, it's worth the effort.

HTH.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 SP1 64bit.

Joanna

  • Full Member
  • ***
  • Posts: 227
Re: IRC channel
« Reply #47 on: November 29, 2022, 04:08:00 pm »
I always thought that there is more memory available on the heap than the stack. Maybe because a “heap” of stuff seems gigantic and a stack seems reasonably small.

Most of my encounters with assembly language are when Lazarus opens it for me to show where the problem occurred. I’ve never really understood it well but it looks interesting.  :D
Come chat on IRC .. https://libera.chat/guides/
IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channels  #fpc #lazarus #pascal 
type /msg nickserv help register to get started

440bx

  • Hero Member
  • *****
  • Posts: 3265
Re: IRC channel
« Reply #48 on: November 29, 2022, 04:42:54 pm »
I always thought that there is more memory available on the heap than the stack. Maybe because a “heap” of stuff seems gigantic and a stack seems reasonably small.
One of the common differences between a heap and a stack is that, normally, the maximum size of a stack is preset at load time whereas a heap can theoretically grow until there is no more address space available.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 SP1 64bit.

Joanna

  • Full Member
  • ***
  • Posts: 227
Re: IRC channel
« Reply #49 on: November 30, 2022, 03:06:41 pm »
I’m going to guess that the assembly language that showed in Lazarus was for the stack because it had jmp commands. Does assembly for heap use jump commands at all. It seems like access to heap memory is more random than the stack which follows a certain path then backtracks back to where it started.
Come chat on IRC .. https://libera.chat/guides/
IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channels  #fpc #lazarus #pascal 
type /msg nickserv help register to get started

ccrause

  • Hero Member
  • *****
  • Posts: 687
Re: IRC channel
« Reply #50 on: November 30, 2022, 04:41:34 pm »
I’m going to guess that the assembly language that showed in Lazarus was for the stack because it had jmp commands.

The jmp instruction has to do with code execution flow, not memory access.  I assume you are referring to the x86 instruction set.

A random discussion on how variables are accessed on the stack (example is in C, but similar principles are used by FPC).

alpine

  • Hero Member
  • *****
  • Posts: 630
Re: IRC channel
« Reply #51 on: November 30, 2022, 06:03:21 pm »
I’m going to guess that the assembly language that showed in Lazarus was for the stack because it had jmp commands. Does assembly for heap use jump commands at all. It seems like access to heap memory is more random than the stack which follows a certain path then backtracks back to where it started.

Simply speaking, the stack is a data structure which can be manipulated with the help of only two methods: PUSH(X) and POP(X).
It follows the LIFO principle (i.e. Last-In-First-Out) what  means that the value of X you pushed last will be pulled by the next pop.

The thing is that the most processors (but not all), have special instructions for doing that, commonly named PUSH and POP. They work with a dedicated register called SP (stack pointer) which points somewhere in RAM. When you push something, the SP decrements and the value goes written at that location. Resp. when you pop - the value where the SP points will be fetched and the SP will be incremented.

This is very convenient when you want to call a subroutine - the PC (program counter) is a register holding the next instruction for the execution by the processor. Calling a subroutine, CALL Sub is actually PUSH PC i.e. save the program counter onto the stack, then JMP Sub i.e. jump to the address of the subroutine. Subroutines normally ends with a RET instruction which is actually POP PC i.e. get the value from the top of the stack and put it into the PC (program counter). That way the processor will resume execution at the point following the CALL Sub instruction.

That same mechanism is used to transfer the subroutine actual parameters along with the return address, e.g. if you call a procedure foo(1,2,3) then the compiler will generate something like:
Code: Text  [Select][+][-]
  1.  PUSH 1 ; First parameter pushed
  2.  PUSH 2 ; Second parameter pushed
  3.  PUSH 3 ; Third
  4.  CALL foo ; Actual call
(this is a very simplified representation, of course)

Then into the procedure itself, the formal parameters can be accessed as: return address - SP[0], Third parameter - SP[1], Second - SP[2], First - SP[3]. This is why when you modify a parameter it doesn't affect the actual variable given at the call - you actually work with a copy of it allocated on the stack (by pushing its value).

After the procedure finishes, the stack must be cleaned up from the formal parameters, this is done different in different languages (also in FPC, depending on the calling mechanism) but the idea is to increment the stack pointer with the same amount as it was decremented at the time after the parameters were pushed. In the example, the inverse of three pushes can be POP AX, POP AX, POP AX - three pops into a scratch register:
Code: Text  [Select][+][-]
  1.  PUSH 1 ; First parameter pushed
  2.  PUSH 2 ; Second parameter pushed
  3.  PUSH 3 ; Third
  4.  CALL foo ; Actual call
  5.  POP AX ; Pop the third parameter
  6.  POP AX ; Pop the second
  7.  POP AX ; Pop the first
  8.  ...
(again, this is a pseudo-code, the actual assembly will differ)

As 440bx said, the stack area (where the SP points) is usually pre-allocated and it is of some predefined size. If you call subroutines with a huge parameters (usually big static arrays) the stack will soon overflow, i.e. the SP will go beyond the boundaries of the pre-allocated RAM chunk.
The same is valid for the local variables of the subroutine - they're also allocated into the stack by manipulating the stack pointer at the time of the call.

All that stack allocation/deallocation is made automatically for you by the FPC compiler. The heap, at the other hand, is different - the allocation is made by you and it is your responsibility to reclaim the memory when it is not needed. When you need some memory then you call GetMem, when you want to free it - FreeMem. When you want a new instance of some class, then you call X:=TSomeClass.Create, when you're done with it, you call X.Free.

The exceptions of that pattern are so called "managed types" - the dynamic arrays and the String type. They also reside at the heap but the compiler takes care for that.

"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

440bx

  • Hero Member
  • *****
  • Posts: 3265
Re: IRC channel
« Reply #52 on: November 30, 2022, 07:06:44 pm »
@Joanna,

You got good replies, one of them particularly comprehensive :)

Basically, if you haven't been exposed to how a computer works (CPU, memory, I/O, etc), there are some things that will be difficult to understand.

Becoming really good in Assembly programming is a time consuming task because the programmer is responsible for every detail (unlike when using a compiler like FPC that does a lot for you automatically) but, a basic idea can be had in a reasonable amount of time.

Here are a couple of tutorials you may find useful (picked those because they are quite short):

https://www.tutorialspoint.com/assembly_programming/assembly_tutorial.pdf
https://www.cs.dartmouth.edu/~sergey/cs258/tiny-guide-to-x86-assembly.pdf

after reading those you'll still be quite far from being proficient in Assembly language but, they are  reasonable places to start to understand the most basic concepts (which will always be good for you to know.)

Disclaimer: I only had a very cursory look at both documents.  All I can say is, they seem ok and cover the basics, nothing beyond that.

HTH.


FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 SP1 64bit.

Handoko

  • Hero Member
  • *****
  • Posts: 4764
  • My goal: build my own game engine using Lazarus
Re: IRC channel
« Reply #53 on: November 30, 2022, 09:24:37 pm »
 :o Are you kidding, someone want to learn Assembly?

I has this code, written when I was 14 if I remember correctly:

Code: Text  [Select][+][-]
  1. ; PROCEDURE TULIS (W, B, K : BYTE; S : STRING);
  2. PUSH  DS
  3. PUSH  SS
  4. POP   DS
  5. MOV   BX, SP
  6. MOV   AX, 0050
  7. MUL   BYTE PTR [BX+0A]
  8. MOV   CX, [BX+08]
  9. XOR   CH, CH
  10. ADD   AX, CX
  11. ADD   AX, AX
  12. MOV   DX, B800
  13. MOV   ES, DX
  14. MOV   DI, AX
  15. MOV   AH, [BX+0C]
  16. LDS   SI, [BX+04]
  17. XOR   CH, CH
  18. MOV   CL, [SI]
  19. CMP   CL, 0
  20. JZ    130
  21. INC   SI
  22. CLD
  23. LODSB
  24. STOSW
  25. LOOP  012C
  26. POP   DS
  27. RET   000A

Not exactly, but you can get similarly result by re-writting the code in Pascal:

Code: Pascal  [Select][+][-]
  1. GotoXY(K, B);
  2. TextColor(W);
  3. Write(S);

My first computer was 8088, very slow. And I only got Turbo Basic. I knew TB can import obj files, so for performance reason I wrote the Assembler codes using DEBUG.COM and saved them to obj files. Then I could get much better performance using TB to write text-based and simple graphics games.

TB built-in functions were slow, BIOS functions were not fast enough. So I had to use Assembly to write functions to map the data directly to the hardware memory.

Luckily I later got Turbo Pascal, version 5 if I remember correctly. Then I never touch Assembly anymore because:
- Pascal is many times faster than BASICA, QBasic, QB, TB, etc
- Pascal allow direct port and memory access

But for nostalgic reason, I keep all the codes I wrote in the past.

Anyways, if anyone wants to learn computer hardware programming, I recommend this book, I had it and it really helped me understand how pc works:
https://www.amazon.in/Peter-Norton-Programmers-Personal-Computer/dp/1556151314
« Last Edit: November 30, 2022, 09:40:42 pm by Handoko »

440bx

  • Hero Member
  • *****
  • Posts: 3265
Re: IRC channel
« Reply #54 on: November 30, 2022, 11:34:08 pm »
:o Are you kidding, someone want to learn Assembly?
I suggested she (Joanna) learn some Assembly language in order to have a better understanding of how a computer and a compiler works.

Knowing at least some assembly language programming is still a very useful and valuable skill.  Of course, I would not recommend writing an entire program in Assembly anymore but, for understanding how the hardware and some software works, it's quite appropriate.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 SP1 64bit.

d.ioannidis

  • Full Member
  • ***
  • Posts: 202
    • Nephelae
Re: IRC channel
« Reply #55 on: November 30, 2022, 11:47:09 pm »
Hi,

:o Are you kidding, someone want to learn Assembly?
< snip >

yeap me :-X ( ... no kidding  :D )

I started, a little while ago, to learn AVR assembly for fun :-[ see here ...

regards,

Joanna

  • Full Member
  • ***
  • Posts: 227
Re: IRC channel
« Reply #56 on: December 01, 2022, 12:58:31 am »
Thanks Everyone for all the useful information.
I’m going to try to read some of those tutorials in assembly. Maybe if the assembler (which I only see when my program has something wrong with it) will not look like jibberish any and give useful hints on what’s wrong. :D
Come chat on IRC .. https://libera.chat/guides/
IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channels  #fpc #lazarus #pascal 
type /msg nickserv help register to get started

Seenkao

  • Sr. Member
  • ****
  • Posts: 431
    • New ZenGL.
Re: IRC channel
« Reply #57 on: December 01, 2022, 04:23:08 am »
:o Are you kidding, someone want to learn Assembly?
Я изучаю. )))
При чём я стараюсь сопоставить между собой несколько ассемблеров. Для более точного понимания, как происходит работа в той или иной архитектуре.
Здесь можно посмотреть код. (код 456123) x86/x86_64/ARM/ARM64 Единственная проблема, что это пока только на яндекс диске. Позже буду выкладывать на GitHub. Когда материала будет побольше.  :-[

Google translate:
I'm studying. )))
At what I try to compare several assemblers with each other. For a better understanding. how it works in a particular architecture.
Here you can see the code. (code 456123) x86/x86_64/ARM/ARM64 The only problem is that it's only on Yandex disk so far. I'll post it on GitHub later. When there is more material. :-[
Rus: Стремлюсь к созданию минимальных и достаточно быстрых приложений.
Работаю над ZenGL.
Eng: I strive to create applications that are minimal and reasonably fast.
Working on ZenGL. :)

Joanna

  • Full Member
  • ***
  • Posts: 227
Re: IRC channel
« Reply #58 on: December 02, 2022, 01:05:52 pm »
I just thought of another question.. is assembly language standardized or are there all sorts of different dialects of assembly language?
Come chat on IRC .. https://libera.chat/guides/
IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channels  #fpc #lazarus #pascal 
type /msg nickserv help register to get started

KodeZwerg

  • Hero Member
  • *****
  • Posts: 904
  • Fifty shades of code.
    • Delphi & FreePascal
Re: IRC channel
« Reply #59 on: December 02, 2022, 01:16:26 pm »
I just thought of another question.. is assembly language standardized or are there all sorts of different dialects of assembly language?
Theres a basic set that works on all CPUs and CPU specific ones. And what code does can be OS dependent.
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

 

TinyPortal © 2005-2018