HelpCOULD SOME1 ANSWER MY QUESTIONS ABOUT THIS ASM CODE?

Posts 1–14 of 14 · Page 1 of 1
COULD SOME1 ANSWER MY QUESTIONS ABOUT THIS ASM CODE?
This is the cpp file:
Code:
 #include <iostream>
#include <conio.h>
#include <ctime>

#include "headerfile.h"

using namespace std;

int main()
{
	int count = 1024;
	char *arr = new char[count];

	/* init the array to contain random numbers just so it's easy to tell that our procedure has worked*/
	for(int i = 0; i < count; i++) arr[i] = (char)rand();

	//Print down the initial values
	for(int j = 0; j < count;j++) cout<<(int)arr[j]<<" ";

	//Call our procedure
	ZeroArray(arr, count*sizeof(char));

	//Print out the values again to make sure they are all 0
	for(int j = 0; j < count;j++) cout<<(int)arr[j]<<" ";
	_getch();        //Prevent the window from closing
	delete[] arr;    //Free our memory	
	return 0;
}
This is the asm file
Code:
 .code
; void ZeroArray(void* RCX, int EDX)
;Sets all bytes from *RCX to *RCX+EDX to 0
ZeroArray proc
cmp edx, 0        ;Check for 0 or less
jle Finished

cmp edx, 1        ;Check for 1
je SetFinalByte

mov ax, 0         ;Set ax to 00
mov r8d, edx      ;Save the original count to r8d
shr edx, 1        ;Halve the count because we are using AX, not AL

MainLoop:
mov word ptr [rcx], ax      ;Sets 2 bytes to 0
add rcx, 2                  ;Moves rcx to the next 2 bytes
dec edx                     ;Decrement our counter
jnz MainLoop                ;Jump if we have more to set

and r8d, 1                  ;Check if there was an even number
jz Finished                 ;If ther was, we are done.

SetFinalByte:
	mov byte ptr [rcx], 0

Finished:
 ret
 ZeroArray endp
 end
This is the header file
Code:
 #ifndef ZERYARRAY_H
#define ZEROARRAY_H

// Set CountInBytes bytes to 0 starting from &arr and moving to &arr + CountInBytes

extern "C" void ZeroArray(void *arr, int CountInBytes);

#endif
Now my Questions:
First : the ASM file:
1:
Code:
 ; void ZeroArray(void* RCX, int EDX)
;Sets all bytes from *RCX to *RCX+EDX to 0
What does he means by those comments?
2:
Code:
 mov ax, 0         ;Set ax to 00
, What does he mean by 00? why not 0?
3:
Code:
 mov r8d, edx      ;Save the original count to r8d
Original count? what does he mean by that? also we don't know what's the value of edx right?
4:
Code:
shr edx, 1        ;Halve the count because we are using AX, not AL
What does he mean by his comment? hauving a 1? what the..??
5:
Code:
mov word ptr [rcx], ax      ;Sets 2 bytes to 0
this one i want u to explain every single code of it , in the commenet why he writes "sets 2 bytes"? i know that in asm "word"=2bytes,"dword"=4bytes, and so on.. , but does he mean by that code: (move the value of ax which has type word to the pointer rcx?) or im wrong? if im wrong PLEASEE explain this code correctly
6:
Code:
add rcx, 2                  ;Moves rcx to the next 2 bytes
Doesn't that mean that he adds 2 to the value of rcx? Why then he says in comment "MOVE TO NEXT 2 BYTES"?? correct me guys and tell me what's the right explanation
7:
Code:
 dec edx                     ;Decrement our counter
Why in the comment he said "counter"? isnt the counter>> cx? why he says dx is the counter?
8:
Code:
 and r8d, 1                  ;Check if there was an even number
What does that mean?

Few little questions hah?
NB: I Finished C++ by watching 75 vidoe tuts, and made lots of project where their folder is 1 gega+, but im newbie to assembly..
Too many questions , I'm busy but I will answer you ..

First take a look at google in asm in C++...

Second thing to answer you about 00 , you must learn CE to know about the bytes..

I can't really help you because I'm busy coding atm...
I could answer all of these questions but these are things you would know if you knew the basics of assembly. If you want to learn assembly, don't start with this, this is too much for someone who knows nothing of assembly.
[QUOTE=meromarololo2]...
Now my Questions:
1:
Code:
void ZeroArray(void* RCX, int EDX);Sets all bytes from *RCX to *RCX+EDX to 0 What does he means by those comments?
Ram is a large continuous row of boxes, numbered 1 - some huge number which is how much ram you have (1 gig, 2, 8, 16 etc).
Set all bytes (starting at some #, and stopping at some #), so like from box # 1,000,000 to # 3,000,00, will all get set to 0.
EDX = the size of the array (ie. the number of bytes to clear)
So RCX to RCX+EDX is like saying 1,000,000 to (1,000,000 + 100) -- for a 100 byte array. Point being, rcx = start address, rcx+edx = stop address.

2:
Code:
 mov ax, 0         ;Set ax to 00  , What does he mean by 00? why not 0?
Anything after the semi-colon is a comment. 00 is the same as 0. The programmer's preference/ typo - not significant. 00 = 0 = 00000000

3:
Code:
 mov r8d, edx      ;Save the original count to r8d  Original count? what does he mean by that? also we don't know what's the value of edx right?
The instruction basically translates to "MOVE into r8d, the value of edx" - ie. make a COPY of edx and store it in r8d.
False. We DO know the value of edx --> in C++ when you pass in arguments to a function, it will place them in registers (first rcx, then rdx -- I don't know off top of my head -- the videos explain calling convention / passing parameters). So in this example, we know...rcx = the first parameter (a memory address to the beginning of the array) and edx = the 2nd parameter, the # of items in the array.

4:
Code:
shr edx, 1        ;Halve the count because we are using AX, not AL What does he mean by his comment? hauving a 1? what the..??
Shifting right 1 bit is the same as dividing by 2. Shifting right 2 bits is dividing by 4, 3 bits = 8, 4 bits = 16 etc etc.
He divides by 2 because we're working with WORDS, which is 2 bytes at a time. So if count was 100, we only need to move 2 bytes 50 times. The more bytes you move at a time, the less times you have to move bytes. If we were doing 4 bytes at a time we'd use "mov dword ptr [rcx], eax ;Sets 4 bytes to 0" and "add rcx 4".

5:
Code:
mov word ptr [rcx], ax      ; but does he mean by that code: (move the value of ax which has type word to the pointer rcx?) or im wrong? if im wrong PLEASEE explain this code correctly
You're correct. So, a WORD is 2 bytes. Assume RCX = some random number --> that number box will get the FIRST BYTE of ax, and that number box + 1 will get the SECOND byte of ax. 2 boxes of ram are used to store the value of ax.

6:
Code:
add rcx, 2                  ;Moves rcx to the next 2 bytes Doesn't that mean that he adds 2 to the value of rcx? Why then he says in comment "MOVE TO NEXT 2 BYTES"??
Registers store numbers. Ram boxes have a number associated with them. See the connection?? If RCX = 5 then reading [rcx] is the same as [5] which will read the value of box 5. By increasing the register, the next time you use [reg], it will point to a different box.

7:
Code:
 dec edx                     ;Decrement our counter ; Why in the comment he said "counter"? isnt the counter>> cx? why he says dx is the counter?
Not sure, haven't looked over the code as a whole. If he said, I'm sure he's using it as a counter...or a typo..idk. Translates to "subtract 1 from register"..basic. edit: looking at his asm prototype, he shows which registers get the values passed in.
"; void ZeroArray(void* RCX, int EDX)" ie rcx = start address, edx = count (size of array)

8:
Code:
 and r8d, 1                  ;Check if there was an even number
What does that mean?[/code]
in BINARY, the RIGHTMOST digit (ie digit 0) can only have a value of 0 or 1 --> if that BIT is set, it means that number is odd. Any odd number will always have the very first bit set to 1. And any even number will always have that bit set to 0. Basic Binary. Calling "and r8d, 1" will check if that bit is set and set the FLAGS register.

-Hope that helps a little. His videos explain it better than I can.
@abuckau907
Thanks,thanks, and thanks, i understand 80% of what u explained, but if just u could explain 3 points also on asm?(at the end of the post)

First : My Questions in the .cpp file :
1:
Code:
void ZeroArray(void* RCX, int EDX)
i understand it except for void* RCX, in c++ i never put void in a parameter, i used it only as a funtion that doesn't return a value , so what does RCX of TYPE void mean?

2:
Code:
ZeroArray(arr, count*sizeof(char));
Here, First: does this arr means the address which *arr points to ? Second: What does he mean by that count* ? Why it's a pointer??

Now the 3 points i wanted to ask about in the asm:
_ First: in Question 5:
why wrote (move word ptr [rcx],ax) ?? why did he point to an address? why not just write mov rcx,ax .. please explain why is the usage of pointer?

_Second in Question1:
Sets all bytes from *RCX to *RCX+EDX to 0
So RCX to RCX+EDX is like saying 1,000,000 to (1,000,000 + 100) -- for a 100 byte array. Point being, rcx = start address, rcx+edx = stop address.
u explained these things ,, but where are they in the code itself??

_Third : in this code:
Code:
 mov ax, 0         ;Set ax to 00
, what he means is that he saves 0 in AL and 0 in AH right?,
if im right, -(and i think im right as in this code
Code:
mov word ptr [rcx], ax
we set 2 BYTES to 0, as we copied the 0 of AL and the 0 of AH)- then if so,
1: What will happen if i wrote
Code:
mov eax, 1
, he will put 1 in AL and 1 in AH, or just 1 in AL, and AX stays 0?
2:
Code:
mov eax, 0
? What will this code do? (since eax dons't consist of 2 parts as AX..{AL,AH}) and
3:What will happen if i wrote this one
Code:
mov eax, 1
Thanks a lot u were really helpful,
[QUOTE=meromarololo;]...

1:
Code:
void ZeroArray(void* RCX, int EDX)
 i understand it except for void* RCX, in c++ i never put void in a parameter, i used it only as a funtion that doesn't return a value , so what does RCX of TYPE void mean?
C++ does it for you. It's a huge complicated subject, and the videos explain it better than I could. Again, anything after a semi-colon is a comment so, "; void ZeroArray(void* RCX, int EDX)" is a comment. It looks VERY similar to a C++ prototype. He did that on purpose, to shows us which 2 registers the 2 parameters would be passed in. That's all dictated by the C++ compiler. RCX has no type, it's just 64 bits of storage. He was just indicating that the first parameter is a void*, ie a memory address, and the 2nd parameters is an 32 bit signed int.

2:
Code:
ZeroArray(arr, count*sizeof(char));
 Here, First: does this arr means the address which *arr points to ? Second: What does he mean by that count* ? Why it's a pointer??
Basic C++ here --> an array name may be used just like a pointer, because it is one.
count = "the numbers of items in the array" * "sizeof(each item)" = the total number of bytes required for the array. If you have 100 integers, that's 100 * 4 = 400 bytes. Since sizeof(char) = 1, in this example it's pointless, but that's how the code would look if you were using a bigger datatype. edit: *, in this case, is not pointer operator, is multiplication.

Now the 3 points i wanted to ask about in the asm:
First: in Question 5:
Code:
 why wrote (move word ptr [rcx],ax) ?? why did he point to an address? why not just write mov rcx,ax .. please explain why is the usage of pointer?
mov rax, 5
mov [rax], 5

See the difference? it's all about the [ ] 's. The []'s mean "move into a memory box" basically. So, the top line just moves the value 5 into rax. The bottom line moves the value 5 into whatever memorybox rax is pointing at (so if rax = 1,000,000 then mov [rax],5 will put the value 5 in box one million). (edit: Actually 4 memory boxes are filled : rax, rax+1, rax+2, rax+3, because we didn't put a size specifier, 5 is stored in 32 bits - 4 bytes).

Code:
 mov ax, 0         ;Set ax to 00 , what he means is that he saves 0 in AL and 0 in AH right?
Yes. Essentially. Since ax is a 16 bit register, and no size specifiers are used, 0 is assumed to be a 16 bit value, so 16 0's are put into ax. I'm not sure it breaks it into a 2 part operation of doing AL, then AH, but maybe. Either way, all 16 bits of ax are set to 0.

Code:
mov word ptr [rcx], ax ;  we set 2 BYTES to 0, as we copied the 0 of AL and the 0 of AH)
Yes.

- then if so,
1: What will happen if i wrote
Code:
mov eax, 1 ; he will put 1 in AL and 1 in AH, or just 1 in AL, and AX stays 0?
No. Since no SIZE SPECIFIERS were used, and since eax = 32 bit, compiler assumes 1 is supposed to take up 32 bits. So it'll be 31 0's and the rightmost bit of 1 that get stored into eax.
Also, AL/AH are only the bottom 16 bits of eax. Since you said mov eax, it's effecting all 4 bytes of eax, not just the bottom 2 that make up al/ah.

2:
Code:
mov eax, 0 ; What will this code do? (since eax dons't consist of 2 parts as AX..{AL,AH})
Same as above -> since no size specifiers were used, the value "0" takes up 32 bits (same as the register) and all 32 bits get copied over. All 0's.

3:What will happen if i wrote this one
Code:
mov eax, 1
Same as above. 1 is assumed to be a 32 bit value. The top 3 bytes of eax = 0 and the bottom byte = 1
eax = 00000000 00000000 00000000 00000001 just like you'd write it in normal binary. Well in normal binary you might just put "01"..or put it at a byte and say "00000001", but since we're using the EAX register, which is 32 bits, and didn't use a size specifier, it assumes (makes!) the data the correct size to fill the register being used. So even though the value 1 only needs 1 bit techincally, because you didn't use size specifier, it bumps it up to the required size (of the register) and makes it 32 bits.
Quote Originally Posted by abuckau907 View Post
Ram is a large continuous row of boxes, numbered 1 - some huge number which is how much ram you have (1 gig, 2, 8, 16 etc).
Hi,

Memory addressing is zero-based. This means the first memory location is at 0, not 1.
Quote Originally Posted by meromarololo2 View Post
Now my Questions:
First : the ASM file:
1:
Code:
 ; void ZeroArray(void* RCX, int EDX)
;Sets all bytes from *RCX to *RCX+EDX to 0
What does he means by those comments?
It means the memory range pointing from the address of RCX to RCX+EDX will be set to 0. In his prototype RCX is the pointer of the address to start writing zero's to. EDX is the count/size of zeros to write. So if EDX was 4, it would be the same thing as:

*(BYTE*)((DWORD_PTR)RCX + 0) = 0;
*(BYTE*)((DWORD_PTR)RCX + 1) = 0;
*(BYTE*)((DWORD_PTR)RCX + 2) = 0;
*(BYTE*)((DWORD_PTR)RCX + 3) = 0;

And so on for however long the size is set to.


Quote Originally Posted by meromarololo2 View Post
2:
Code:
 mov ax, 0         ;Set ax to 00
, What does he mean by 00? why not 0?
It's the same thing. 00 is the same as 0 in this case. So just ignore the extra 0 in the comment.


Quote Originally Posted by meromarololo2 View Post
3:
Code:
 mov r8d, edx      ;Save the original count to r8d
Original count? what does he mean by that? also we don't know what's the value of edx right?
EDX was the size param in the function call. It is being stored r8d to be used later checking if it has completed writing to the array.


Quote Originally Posted by meromarololo2 View Post
4:
Code:
shr edx, 1        ;Halve the count because we are using AX, not AL
What does he mean by his comment? hauving a 1? what the..??
SHR stands for shift-right. It is a short-hand method of dividing. Doing a SHR with 1 as the right-hand operator states to divide the left-hand operator by 2.
So this is saying:
EDX = EDX / 2


Quote Originally Posted by meromarololo2 View Post
5:
Code:
mov word ptr [rcx], ax      ;Sets 2 bytes to 0
this one i want u to explain every single code of it , in the commenet why he writes "sets 2 bytes"? i know that in asm "word"=2bytes,"dword"=4bytes, and so on.. , but does he mean by that code: (move the value of ax which has type word to the pointer rcx?) or im wrong? if im wrong PLEASEE explain this code correctly
mov is the move/copy instruction. It takes the right-hand operators value and copies it into the left-hand location.
word ptr means that the value being copied from the right is being stored as a word (word is 2 bytes long).

ax is 16bits of the EAX register. In the 32bit instruction family, the registers: EAX EBX ECX EDX ESI EDI EBP EIP ESP are all made up of 32bits. (32bits = 4 bytes = DWORD)
Each data register also has a 16bit and two 8bit representations. (EAX EBX ECX EDX)
For example EAX is made up of: EAX (32bit) -> AX (16bit) -> AH and AL (8bit)

To explain it better, if you set EAX to 0x12345678 then this is what each value would be:
EAX = 0x12345678
AX = 0x5678
AH = 0x56
AL = 0x78



Quote Originally Posted by meromarololo2 View Post
6:
Code:
add rcx, 2                  ;Moves rcx to the next 2 bytes
Doesn't that mean that he adds 2 to the value of rcx? Why then he says in comment "MOVE TO NEXT 2 BYTES"?? correct me guys and tell me what's the right explanation
RCX is currently a pointer, adding 2 will adjust the pointer position by 2 places. But yes, it is just adding two to the value of what RCX holds.


Quote Originally Posted by meromarololo2 View Post
7:
Code:
 dec edx                     ;Decrement our counter
Why in the comment he said "counter"? isnt the counter>> cx? why he says dx is the counter?
EDX holds the count of bytes to zero in the array. He used the word counter pertaining to the variable, not the register.


Quote Originally Posted by meromarololo2 View Post
8:
Code:
 and r8d, 1                  ;Check if there was an even number
What does that mean?
Because he is stepping two bytes each write (hes writing 0x00 0x00 each time) he checks if the overall count is odd or even. If it is odd, he needs to write the last byte alone as a single byte and not a word.
@abuckau907 @atom0s
Thanks Aloooooot Guys, now i understand 95% of it, even check the comments i wrote on the ASM : (copy the img link and paste it in a new tab to see full img)


and this one is the cpp



,anyway, ima ask about the 5% i don't understand:

1:
Code:
char *arr = new char[count];
This one, i took in C++ about the char type, it's like: char Apple = 'A' ,,so when we call 'A' the Apple will appear , so can u explain that in code

2:
Code:
mov word ptr [rcx], ax      ;;Sets 2 bytes to 0 (as we chose ax(which has 2 zeros) and word(whichs means we save the 2 zeros in 2 bytes)  we cant say [rcx] only, as by that we will change the memory address of rcx to 0.., but ptr[rcx] means the to change the value of that memory address to 0.
Is my comment right?


Thx aloot now im over with this shitty project
Quote Originally Posted by meromarololo View Post
1:
Code:
char *arr = new char[count];
This one, i took in C++ about the char type, it's like: char Apple = 'A' ,,so when we call 'A' the Apple will appear , so can u explain that in code
char is a single character.
char* is a pointer to a character array.

Using the 'new' keyword states that you want to create an array of the right-hand type, which will return a pointer to that array after it is created.
So with 'new char[count]', if count is set to 10, it will allocate 10 bytes of space and return the pointer to it to be used as a char array.


Quote Originally Posted by meromarololo View Post
2:
Code:
mov word ptr [rcx], ax      ;;Sets 2 bytes to 0 (as we chose ax(which has 2 zeros) and word(whichs means we save the 2 zeros in 2 bytes)  we cant say [rcx] only, as by that we will change the memory address of rcx to 0.., but ptr[rcx] means the to change the value of that memory address to 0.
Is my comment right?
Keep in mind ax is not always going to be 0. You have to set it to 0 yourself if you want it to be. Hence the line:
mov ax, 0
1:
Code:
char *arr = new char[count];
char *arr = new char[count]; char* is a pointer to the content of the string which is = new char[count] , new char is casting / converting count to char if it wasn't and allocating an new char in your RAM , but after using it I recommended to free the memory with delete[] arr;

2:
Code:
mov word ptr [rcx], ax ;Sets 2 bytes to 0
A bytes is as normal looking like this , A0 this is a single byte , A0 00 = 2 bytes.


&
Code:
add rcx, 2 ;Moves rcx to the next 2 bytes
Same as the second explanation .
@Transformer-
1:
char* is a pointer to the
WTF?? char is a pointer?? isn't char a type and arr is the pointer???? ??? how could char be the pointer? did u misunderstand or am i wrong??
content of the string
What string? we didn't even define one..

i still don't understand this code..

& nvm abt the other 2 questions, i deleted em from ma post.

2:
BTW, is my comment here Right?? :
Code:
mov word ptr [rcx], ax      ;Sets 2 bytes to 0 (as we chose ax(which has 2 zeros) and word(whichs means we save the 2 zeros in 2 bytes)  we cant say [rcx] only, as by that we will change the memory address of rcx to 0.., but ptr[rcx] means that we change the value of that memory address to 0.
NB: THE COMMENT IS 3 LINES LOL, SCROLL DOWN TO CHECK THE LAST LINE
Quote Originally Posted by meromarololo View Post
@Transformer-
BTW, is my comment here Right?? :
Code:
mov word ptr [rcx], ax      ;Sets 2 bytes to 0 (as we chose ax(which has 2 zeros) and word(whichs means we save the 2 zeros in 2 bytes)  we cant say [rcx] only, as by that we will change the memory address of rcx to 0.., but ptr[rcx] means that we change the value of that memory address to 0.
i think it is right as we know byte is 1 byte, word is 2 bytes, great job !

as long as the value in the register 'ax' contains 00, it will set the address's value to 0.
else it will just set it to the value that contains it
lol hes banned?
If only i could code
Posts 1–14 of 14 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us