News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

use the registry correctly

Started by elettronico_79, October 08, 2010, 05:35:43 PM

Previous topic - Next topic

elettronico_79

Hi, i have find that code: and i would ask why was used the registry AL, AX and EAX if writing for example in EAX there is the possibility to compromise AX and AL?
I hope you have understand what I mean! Sorry my english!

Thanks!


program DemoMOVaddSUB;



#include( "stdlib.hhf" );



static

    i8:     int8    := -8;

    i16:    int16   := -16;

    i32:    int32   := -32;



begin DemoMOVaddSUB;


    stdout.put

    (

        nl,

        "Initialized values: i8=", i8,

        ", i16=", i16,

        ", i32=", i32,

        nl

    );

    mov( 0, al );   // Compute i8 := -i8;

    sub( i8, al );

    mov( al, i8 );

   

    mov( 0, ax );   // Compute i16 := -i16;

    sub( i16, ax );

    mov( ax, i16 );

   

    mov( 0, eax );  // Compute i32 := -i32;

    sub( i32, eax );

    mov( eax, i32 );



    // Display the absolute values:



    stdout.put

    (

        nl,

        "After negation: i8=", i8,

        ", i16=", i16,

        ", i32=", i32,

        nl

    );


end DemoMOVaddSUB;

Vortex

You should post your question to the HLA Forum to receive more support.

elettronico_79

sorry, i'm new.
could you move that post tho HLA forum, please?
thanks!

BogdanOntanu

Quote from: elettronico_79 on October 08, 2010, 07:11:38 PM
sorry, i'm new.
could you move that post tho HLA forum, please?
thanks!

Done
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

clive

Quote from: elettronico_79
Hi, i have find that code: and i would ask why was used the register AL, AX and EAX if writing for example in EAX there is the possibility to compromise AX and AL?
I hope you have understand what I mean! Sorry my english!

The x86 supports working on 8, 16 and 32-bit quantities, and the latest generations will also support 64-bit.

An operation on EAX will impact AL,AH,AX as well. You have to be careful to use operations consistently so you don't get unexpected results.

However an 8-bit action on AL will not impact AH, and one on AH will not impact AL. If you are manipulation 8-bit ASCII characters for example, it is not necessary to use 32-bit operations, or 16-bit for that matter. It is sometimes more efficient these days to do 32-bit operations, but again you must understand numbers will not overflow the same way, and sign comparisons will be different.
It could be a random act of randomness. Those happen a lot as well.

Sevag.K


in this example, there are 3 static variables.  one of 8bits, one of 16 bits and one of 32bits.  in order to manipulate these variables, you have to use registers that are corresponding size.

note that you can use registers that are bigger than what you need, but you have to use type casting in those cases.

you also asked about compromising values.  this does happen but that is not important in this code since it does not need to retain values.

writing to EAX will compromise AX, AH and AL.
writing to to AX will compromise AH and AL, but it will leave the high order bit in EAX alone.
AH and AL are independent to each other.

this is roughly how they look

[                         EAX                                ]
                                [             AX              ]
                                [     AH     ][    AL      ]


elettronico_79

but how can I know what global use registry i should use?
for example, if a program (like Media Player) is using all or o part of EAX and I i write in it with my program, media player will crash? How can i know what regitry are in use and i can't touch them?

thanks!

oex

You mean 'registers' not 'registry' and you have access to all registers in your application you are not sharing them with other applications like Media Player

if you move a value to al you will however overwrite part of an eax value as al is part of eax
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

clive

Quote from: elettronico_79
but how can I know what global use registry i should use?
for example, if a program (like Media Player) is using all or o part of EAX and I i write in it with my program, media player will crash? How can i know what regitry are in use and i can't touch them?

It's probably using EAX and ALL the other available REGISTERS (EBX, ECX, EDX, ESI, EDI, EBP, ESP, etc), it is however running in a different process context, so the registers it is using is of no concern to you.
It could be a random act of randomness. Those happen a lot as well.

elettronico_79

is the operative sistem that manage the registres?

clive

Quote from: elettronico_79 on October 10, 2010, 05:48:20 PM
is the OPERATING SYSTEM that manage the REGISTERS?

I wouldn't say the OS manages them, that's more of the applications job, the OS provides each process/thread with it's own CPU context in which to run. The multi-tasking OS switches between different CPU contexts, so each application appears to have the CPU to itself.

It is your job as the application writer to allocate the registers for your own purposes, and to save or reuse them as needed. Certain API and subroutines will use/destroy/preserve registers, either because they are following a register use/passing convention or because you have written them that way.
It could be a random act of randomness. Those happen a lot as well.

Sevag.K

Quote from: elettronico_79 on October 10, 2010, 05:48:20 PM
is the operative sistem that manage the registres?

yes, when the operating system switches from one task to another, it saves register content and cpu state and restores it when the original task resumes.

you just have to worry about register use in your own applications, *and multi-threading* but you can worry about that later after you become comfortable with programming assembly language.

elettronico_79

when i'm writing that message is firefox using some registers or it's only loaded in ram?

clive

Quote from: elettronico_79 on October 11, 2010, 05:29:16 PM
when i'm writing that message is firefox using some registers or it's only loaded in ram?

The program, web pages and images are in RAM, the program is using the registers in the CPU to process the data. Each key press as you type involves a significant amount of processing before it gets to Firefox, and then Firefox and the OS expend considerably more processing to format and edit them and form the message to display to the screen. All this goes through the processor and uses registers. The processor is doing several trillion instructions in the time it takes you to type a couple of characters. Firefox was probably written in C or C++ and compiled into 80x86 machine code. An assembler can take your source code written in assembler and convert that directly into machine code. The machine code directs the processor to perform specific operations on registers and/or memory. The CPU should be considered as a complex and programmable state machine.

Perhaps you should review some books on Computer Science and Computer Architecture before embarking on assembler programming?

It could be a random act of randomness. Those happen a lot as well.

dedndave

what you must understand is that the register set is unique for each process that the OS is running
the processor "saves" (in a sense) the registers before switching to a different process
you may also want to know that the windows API requires certain registers/flags to be preserved across calls
so, for anything that may be involved in a call-back function, you should preserve the following registers:

EBX
EBP
ESI
EDI
DF (direction flag) should always remain cleared (UP direction)

these registers may be trashed, and are often used to return result values:

EAX
ECX
EDX