News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

new member introduction and advice

Started by q12, September 04, 2021, 12:42:56 PM

Previous topic - Next topic

TouEnMasm

Intel speak of general memory who can be access with a pointer on it.
He can be heap memory or other type ,no matter.
you put the pointer on a register.
Quote
invoke HeapAlloc,HandleHeap,NULL,1000h
mov ebx,eax
mov edx,[ebx]     ;load a register with a  memory location
other type
Quote
.data
roudou dword 0
.code
mov ebx,offset roudou
mov byte ptr [ebx],1                         ;first byte of dword roudou begin one
Fa is a musical note to play with CL

mineiro

Inside processors exist memory.
Registers are one specific type of memory inside processor.

To access the contents of an address in RAM memory, a processor need lock bus data/control/address, access that data, and unlock bus. This spends time.
To increase this access time, enginers created inside a processor a cache memory. (L1,L2,L3). This cache memory is not enough being necessary RAM memory. Access to L1 cache is quickly, L2 a silver medal, L3 a bronze medal, followed by RAM competitors. Intention is that a bit of memory is inside processor instead of outside.

Some microcontrolers call "registers" specific memory location in RAM memory. We can label eax register the first memory location and they size is 4 bytes.

If I move number 4 to eax register. I'm moving what? A memory address or number 4 (immediate)? Who define this are us, programmers.

When dealing with X86 processors we need pay attention to move data between memory locations. In better words, I got one data from a memory location, moved that data to a register, and moved that data from a register to other memory location.
When you read Intel manual you will see that most instructions can deal with register to register, register to memory, memory to register. What about memory to memory!?
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

q12

Thank you mister @mineiro
Excelent explanation. In fact, last night until morning and right after I jumped from bed, I google the shit out of this problem.
And I find some very interesting answers that also confirms what you just said here.
This is my conclusion after reading everything:
Does a CPU have its own RAM?
The short answer: YES
It is a form of memory that is called Register.
It is very small size, starting with 1bit then 1byte(8bits) to 16,32 or 64bytes.
They are the fastest ram implementation in a computer. They live inside a CPU.
Some are adresable (read&write), some are read only or write only.
They hold immediate data, adresses, and pointers.
It is SRAM implementation (static ram and is the fastest and easy to build)

then you said:
Quote
...When you read Intel manual you will see that most instructions can deal with register to register, register to memory, memory to register....
Then this distinction between register and memory is a bit confusing. It should be the same thing.
The memory they are mentioning can be 2 things: 1-a memory in RAM OR 2-a register that is used as it would be a memory.
Because a register have multiple significations, they are "specific" by saying it is a memory. But they should name them as registerMemory (for when it is used like that). This is my personal interpretation after a ton of reading.
Thank you for your answer !

mineiro

hello sir q12;
A processor operates at certain clock pulses by a quartz cristal (frequency, hertz). Processors operates at so much higher frequencies, while RAM memory at not so high. So, I suppose register and memory can be differenciated. To a begginer the cache memory is invisible, so, if that confuse you, forgot that.
The term memory told in manual can be viewed as a data source. Can be RAM memory, ROM memory, a hard disk, a stick, ... .  What is important is, to you read/write to a hard disk, or a RAM memory you will use same processor instruction. The initial concept of file in linux as an example is "everything is a file". I prefer change to "everything is a data".

You told about PIC. You perceived that a lot of instructions only changed name (opcode/mnemonic). PIC have flow instructions (compare, jump conditional/inconditional), arithmetic (add,sub,mul,div), boolean (and,or,not,xor), ... . One big difference is that PIC is RISC, while X86 is CISC. Well, if you disassemble a code, PIC hexadecimal dump of each instruction (opcode) have 2 bytes, maybe 4 bytes, forgot now. If you look to x86, one instruction can have 1 byte, while other 2 bytes, and others N bytes, it's a zoo. I suppose that PIC use specific memory (RAM) address as registers. Good and bad, if you write to a specific memory location is the same as write in a register.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

q12

So far, this is how I understand the mechanism:
for example:
    mov eax,100     ;copy 100 into eax register
    mov ecx,250     ;copy 250 into ecx register
    add ecx, eax

inject an immediate value (of 100) into eax (an Accumulator for operands and results data)
inject an immediate value (of 250) into ecx (a Counter for string and loop operations)
add these 2 registers and the result store it into ecx (because is the left operator) and also it is (a Counter for string and loop operations)?
My biggest issue, is WHY and WHEN to use eax, or ecx ? What is the logic for using 1 register or another?
What is his reason to choose these 2 registers and not other ones, like for example ebx and edx ? It is because the other registers are all pointer registers? By the look of it. Or it's an arbitrary reason? Choose whatever you want, it doesnt matter? (type of logic)
I observe that only eax and ecx are NOT pointers. But the rest ARE pointers.
This detail is absolutely vital?
- Can we can treat every register (the first 6 of them) as general good for anything?
Or
- Can we treat them for their specific type of operation that is assigned to?

The following is a summary of special uses of general-purpose registers:
* EAX - Accumulator for operands and results data
* EBX - Pointer to data in the DS segment
* ECX - Counter for string and loop operations
* EDX - I/O pointer
* ESI - Pointer to data in the segment pointed to by the DS register; source pointer for string operations
* EDI - Pointer to data (or destination) in the segment pointed to by the ES register; destination pointer for string operations
the last 2 are not to be touched:
* ESP - Stack pointer (in the SS segment)
* EBP - Pointer to data on the stack (in the SS segment)

Thank you !

FORTRANS

Quote from: q12 on September 09, 2021, 02:25:50 AM
My biggest issue, is WHY and WHEN to use eax, or ecx ? What is the logic for using 1 register or another?

   My suggestion to you is to download:

Intel Architecture Software Developer's Manual Vol 1 Basic Architecture
Intel Architecture Software Developer's Manual Vol 2 Instruction Set Reference

Or the AMD equivalent.  To start off I would recommend a Google search on;

386INTEL.PDF

That is the 1986 version.  A bit simpler version than the more current versions.

Quote
What is his reason to choose these 2 registers and not other ones, like for example ebx and edx ? It is because the other registers are all pointer registers? By the look of it. Or it's an arbitrary reason? Choose whatever you want, it doesnt matter? (type of logic)

   Looking up an instruction in the reference will show what registers a given
instruction can use.

Quote
I observe that only eax and ecx are NOT pointers. But the rest ARE pointers.

   EAX and ECX can be used to address memory, or as you say be a pointer.

Quote
This detail is absolutely vital?
- Can we can treat every register (the first 6 of them) as general good for anything?
Or
- Can we treat them for their specific type of operation that is assigned to?

   EAX, EBX, ECX, EDX, EDI, ESI, and EBP can be used as "general purpose"
registers for instructions like MOV or ADD.

   EAX is used for MULtiply, DIVide, and string source or destination.  and a number
of "special" instructions (CWD, IN, OUT, ...).

   BX in 16-bit code was a base pointer.  OBE.

   ECX is used as a counter in LOOP and string instructions.

   EDX is used in IN, OUT, MUL, and DIV and some special instructions.

   EDI and ESI have defined roles in string instructions.

   BP in 16-bit code was a base pointer, either to access variable on the stack,
or with a segment override for data.

HTH,

Steve N.

q12

to mister FORTRANS
I am reading: INTEL 80386 PROGRAMMER'S REFERENCE MANUAL 1986, as you recommended.
It is better explained !
I trust old books more than anything. And I trust your recommendation.
Thank you.

I'm also starting to understand that assembler is like Lego. At least is a very young impression. Im still not there yet, but it starts to formulate something in my mind, on how to use the instructions and why. A little bit. All these ideas after the copying exercise and reading. They sum up. My impression is that you build, like with bricks, each line of code, literally. It is a very literal language in a sense. Again, one of my first impressions.
I will have to make a series of exercises to actually start thinking (in assembler) when coding. As little as I understand it, with its bricks.

I also find the answer to my question:
"
My biggest issue, is WHY and WHEN to use eax, or ecx ?
What is the logic for using 1 register or another?
Or it's an arbitrary reason? Choose whatever you want, it doesnt matter? (type of logic)
"
From INTEL 80386 :
2.3.1 General Registers
The general registers of the 80386 are the 32-bit registers EAX, EBX, ECX,
EDX, EBP, ESP, ESI, and EDI. These registers are used interchangeably to
contain the operands of logical and arithmetic operations. They may also be
used interchangeably for operands of address computations (except that ESP
cannot be used as an index operand).

My conclusion:
So these registers are "general" also in the sense to use them and change between them as you please when coding !
They are more like some temporary variables. This was the answer I was looking. I did get the answer, one way or another.

hutch--

Beware of old x86 info, in Win32 you can use all of the 32 bit registers, you just have to know which to protect and which you can freely modify. I mentioned it before, the Intel Application Binary Interface. Where you MUST use specific registers is with instructions that require it, MOVS, STOS and a few others. They use ESI and EDI. Some instructions only work with EAX. You look these up in the Intel instruction manual.

Until you know enough do not try and use EBP and ESP as they are used to construct stack frames.

q12

I understand mister hutch.
So, I should concentrate on the first 4 of them: EAX, EBX, ECX,EDX,
and the last 4 (EBP, ESP, ESI, and EDI) to not touch until I will be more experienced. Very good advice then.
It is exactly what I want to know- what to do and what not to do. That's how I learn.
Thank you again.

hutch--

No, you are making unsound assumptions. Freely use EAX ECX and EDX but preserve EBX ESI and EDI as I showed you. Leave ESP and EBP alone as they provide the stack frame.

q12

I think I understand you now.
So only these 3: EAX ECX and EDX are OK to use.
EBX ESI and EDI to protect them as you put it. So not to touch.
ESP and EBP to not touch them ever.
Correct?

QuoteNo, you are making unsound assumptions.
Also, I am unable to make unsound assumptions. I am only trying (my best) to understand what is going on.
I only follow the data I can see, and that was the data I could gather by reading from all these books.
And of course I can not retain EVERYTHING. Some things may slip. But they are honest mistakes.
Don't interpret me as doing anything bad voliciously.
I am telling you the truth, either you believe me or not.
And also I am trying my best. Do you know how hard is this for me as a beginner? I'm telling you, it is hard.
If I didnt complain until now, now  I will.

Vortex

#41
Hi q12,

Here is a message box example for you :

include \masm32\include\masm32rt.inc

.data

message1  db 'This is test 1',0
message2  db 'Another test',0
caption1  db 'Hello',0
caption2  db 'Buenos dias',0

.code

start:

    invoke  MessageBox,0,ADDR message1,ADDR caption1,MB_OK

    push    MB_OK
    push    OFFSET caption2
    push    OFFSET message2
    push    0

    call    MessageBox

    invoke ExitProcess,0

END start

You can directly push the location \ OFFSET of a string to the stack. No need of the mov eax,OFFSET message1 and push eax pair.

Are OFFSET and ADDR the same thing?

https://masm32.com/board/index.php?topic=4994.0

The MessageBox API :

https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messagebox

Iczelion's tutorials :

https://masm32.com/download/icztutes.exe

https://masm32.com/website/iczelion/iczelion.zip

QuoteTutorial 2: MessageBox

Iczelion's tutorial set is very good to learn assembly language for Windows programming. It also covers coding GUI like windows with controls and dialog boxes.

Attached is the message box sample.

six_L

Hi q12,
Welcome to here.
If you don't understand  some codes, try to use x64dbg(http://x64dbg.com), runnig the codes step by step, x64dbg will show you more informations.

Regards.
Say you, Say me, Say the codes together for ever.

Vortex

Hi q12,

Quote
So only these 3: EAX ECX and EDX are OK to use.
EBX ESI and EDI to protect them as you put it. So not to touch.
ESP and EBP to not touch them ever.

eax,ecx and edx are general purpose registers. They are OK to use.

ebx,esi,edi and ebp can be modified with the condition that you preserve their original values before exiting a procedure. They are non-volatile registers :
Quote
Callee-saved (non-volatile) registers
The other registers are used to hold long-lived values (non-volatile), that should be preserved across calls.

In other words, when the caller makes a procedure call, it can expect that those registers will hold the same value after the callee returns.

Thus, making it the callee's responsibility to both save (push at the beginning) and restore (pop accordingly) them before returning to the caller. As in the previous case, this practice should only be done on registers that the callee changes.

https://en.wikipedia.org/wiki/X86_calling_conventions

The value of eax,ecx,edx are destroyed across Windows API calls. This is not the case while handling esi,edi,ebx and ebp, the non-volatile registers.

esp should not be modified as it's responsibility is to address the stack frame.

avcaballero

Here my thought. Don't expect to learn everything in one day, just take it easy. Don't do everything in the academic way, just do it to work and keep on learning in better ways.