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...
This is the cpp file:
This is the asm fileCode:#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 header fileCode:.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
Now my Questions: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
First : the ASM file:
1:What does he means by those comments?Code:; void ZeroArray(void* RCX, int EDX) ;Sets all bytes from *RCX to *RCX+EDX to 0
2:, What does he mean by 00? why not 0?Code:mov ax, 0 ;Set ax to 00
3:Original count? what does he mean by that? also we don't know what's the value of edx right?Code:mov r8d, edx ;Save the original count to r8d
4:What does he mean by his comment? hauving a 1? what the..??Code:shr edx, 1 ;Halve the count because we are using AX, not AL
5: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 correctlyCode:mov word ptr [rcx], ax ;Sets 2 bytes to 0
6: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 explanationCode:add rcx, 2 ;Moves rcx to the next 2 bytes
7:Why in the comment he said "counter"? isnt the counter>> cx? why he says dx is the counter?Code:dec edx ;Decrement our counter
8:What does that mean?Code:and r8d, 1 ;Check if there was an even number
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..
Last edited by meromarololo2; 07-07-2013 at 08:51 AM.
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: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).Code:void ZeroArray(void* RCX, int EDX);Sets all bytes from *RCX to *RCX+EDX to 0 What does he means by those comments?
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:Anything after the semi-colon is a comment. 00 is the same as 0. The programmer's preference/ typo - not significant. 00 = 0 = 00000000Code:mov ax, 0 ;Set ax to 00 , What does he mean by 00? why not 0?
3:The instruction basically translates to "MOVE into r8d, the value of edx" - ie. make a COPY of edx and store it in r8d.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?
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: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.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..??
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: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.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
6: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.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"??
7: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.Code:dec edx ;Decrement our counter ; Why in the comment he said "counter"? isnt the counter>> cx? why he says dx is the counter?
"; void ZeroArray(void* RCX, int EDX)" ie rcx = start address, edx = count (size of array)
8:What does that mean?[/code]Code:and r8d, 1 ;Check if there was an even number
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.
Last edited by abuckau907; 07-07-2013 at 12:32 PM.
'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
--
meromarololo (07-07-2013),meromarololo2 (07-07-2013)
@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: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?Code:void ZeroArray(void* RCX, int EDX)
2: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??Code:ZeroArray(arr, count*sizeof(char));
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 0u explained these things ,, but where are they in the code itself??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.
_Third : in this code:, what he means is that he saves 0 in AL and 0 in AH right?,Code:mov ax, 0 ;Set ax to 00
if im right, -(and i think im right as in this codewe set 2 BYTES to 0, as we copied the 0 of AL and the 0 of AH)- then if so,Code:mov word ptr [rcx], ax
1: What will happen if i wrote, he will put 1 in AL and 1 in AH, or just 1 in AL, and AX stays 0?Code:mov eax, 1
2:? What will this code do? (since eax dons't consist of 2 parts as AX..{AL,AH}) andCode:mov eax, 0
3:What will happen if i wrote this oneThanks a lot u were really helpful,Code:mov eax, 1
Last edited by meromarololo; 07-07-2013 at 05:29 PM.
[QUOTE=meromarololo;]...
1: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.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:Basic C++ here --> an array name may be used just like a pointer, because it is one.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??
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:
mov rax, 5Code: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
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).
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 ax, 0 ;Set ax to 00 , what he means is that he saves 0 in AL and 0 in AH right?
Yes.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 wroteNo. 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.Code:mov eax, 1 ; he will put 1 in AL and 1 in AH, or just 1 in AL, and AX stays 0?
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: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.Code:mov eax, 0 ; What will this code do? (since eax dons't consist of 2 parts as AX..{AL,AH})
3:What will happen if i wrote this oneSame as above. 1 is assumed to be a 32 bit value. The top 3 bytes of eax = 0 and the bottom byte = 1Code:mov eax, 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.
Last edited by abuckau907; 07-08-2013 at 01:31 AM.
'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
--
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.
It's the same thing. 00 is the same as 0 in this case. So just ignore the extra 0 in the comment.
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.
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
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
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.
EDX holds the count of bytes to zero in the array. He used the word counter pertaining to the variable, not the register.
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.
- Gone; this is another shit forum with children as administrators. Not worth contributing to.
meromarololo (07-08-2013)
@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: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 codeCode:char *arr = new char[count];
2:Is my comment 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 the to change the value of that memory address to 0.
Thx aloot now im over with this shitty project
Last edited by meromarololo; 07-08-2013 at 08:03 PM.
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 .
Last edited by Transformer-; 07-08-2013 at 06:41 PM.
@Transformer-
1:
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??char* is a pointer to the
What string? we didn't even define one..content of the string
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?? :
NB: THE COMMENT IS 3 LINES LOL, SCROLL DOWN TO CHECK THE LAST LINECode: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.
Last edited by meromarololo; 07-08-2013 at 08:30 PM.
Last edited by pedbera; 07-08-2013 at 08:25 PM.
lol hes banned?
If only i could 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.
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
- Gone; this is another shit forum with children as administrators. Not worth contributing to.