At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
word size is 32 bit unsigned. For example, inputting 3 and 10 would get 3->0000 0000 0000 0000 0000 0000 0000 0011 10->0000 0000 0000 0000 0000 0000 0000 1010 I'm going to omit those extra zero's for brevity's sake, and this is in binary (((((((11 xor 100) xor 101) xor 110) xor 111) xor 1000) xor 1001) xor 1010) which equals 1000, which is 8 in decimal.
*I mean, dword
I am currently trying to learn some assembly, so I wrote a little program in ASM, using libraries that are provided for me in the book. These libaries do I/O for me - nothing more really.
I'm using MASM as my assembler.
Here is the source code: INCLUDE Irvine32.inc .data prompt BYTE "Enter a long unsigned integer.",0 ;our prompt .code main PROC call Crlf mov edx,OFFSET prompt call WriteString ;writes the null -terminated string with the offset in edx call ReadDec ;reads an unsigned 32 bit integer from the keyboard, and returns it in eax call Crlf ;writes end of line sequence to the console window. call Crlf mov ebx,eax mov edx,OFFSET prompt call WriteString call ReadDec call Crlf call Crlf ;first value is ebx, and second is eax. eax>ebx cmp eax,ebx jz skip sub eax,ebx mov ecx,eax mov eax,ebx L1: inc eax xor ebx,eax dec ecx cmp ecx,0 jnz L1 mov eax,ebx skip: call WriteDec call DumpRegs ;displays register values in the console window exit main ENDP END main
If you want the libraries yourself, here they are http://asmirvine.com/asm/examples/index.htm
Here is the source of a revised version. INCLUDE Irvine32.inc .data prompt BYTE "Enter a long unsigned integer.",0 .code main PROC call Crlf mov edx,OFFSET prompt call WriteString call ReadDec call Crlf call Crlf mov ebx,eax mov edx,OFFSET prompt call WriteString call ReadDec call Crlf call Crlf mov ecx,eax ;first value is ebx, and second is ecx. ecx>ebx sub ecx,ebx push ebx push ecx mov eax,0 inc ecx L1: xor eax,ebx inc ebx loop L1 pop ecx pop ebx call WriteDec call DumpRegs exit main ENDP END main
My question is, is the second significantly faster than the first? And for what cases?
And, btw, any number xor itself inputs the number itself (1023 xor 1023=1023)
@dpaInc , having fun reading? :3
@inkyvoyd xor AX, AX is used to set the register's value to zero because 1 xor 1= 0 0 xor 0 =0 1 xor 0 = 1 0 xor 1 = 1
and i think 1st one is more faster then the 2nd one
the 2nd program will take more time in push and pop data in stack. so its slow ...
@annas , without the push and the pop, would the second be faster?
It says in my book I am supposed to save register values, so I included the push and pop. They aren't necessary though, right?
yes exactly inky ... the only purpose is to save register
Any other people's comments?
If you assume that all instructions take a single processor cycle (thus assuming no hazards and/or memory stalls), determining which one is better is a case of just counting the instructions. Your first program uses 6 instruction out of the loop (ignoring IO, since both programs have to do that) and 5 instructions inside the loop. That makes a total of 6+5n instructions (where n is the number of xors you need to do). That is from "cmp eax, ebx" up to the skip label. The second program uses 7 + 3n instructions. From "sub ecx, ebx" to "pop ebx". Thus, the second program appears to be faster indeed. The impact of memory misses can be quite huge, but I don't know whether or not the pushes or pops are likely to cause one. One more remark is that I'm not sure the second program will actually work. The "loop" instruction will keep on jumping as long as the ecx register is not-zero. But within the loop, you never change ecx, so it'll result in an infinite loop, I guess.
@slotema , it works perfectly fine for me. I'm using a book that is based on MASM - might be syntax that is different from the assembler you are using.
Ah sorry. I missed a bit where the loop instruction also decrements the ecx register. Sorry for that.