Ask your own question, for FREE!
Computer Science 23 Online
OpenStudy (inkyvoyd):

Wrote two solutions to a programming problem. The problem was to find the XOR between all of the whole numbers between two whole numbers. The first whole number must be smaller than the second.

OpenStudy (inkyvoyd):

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.

OpenStudy (inkyvoyd):

*I mean, dword

OpenStudy (inkyvoyd):

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.

OpenStudy (inkyvoyd):

I'm using MASM as my assembler.

OpenStudy (inkyvoyd):

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

OpenStudy (inkyvoyd):

If you want the libraries yourself, here they are http://asmirvine.com/asm/examples/index.htm

OpenStudy (inkyvoyd):

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

OpenStudy (inkyvoyd):

My question is, is the second significantly faster than the first? And for what cases?

OpenStudy (inkyvoyd):

@farmdawgnation @annas

OpenStudy (inkyvoyd):

@imranmeah91 @nbouscal

OpenStudy (inkyvoyd):

@cshalvey

OpenStudy (inkyvoyd):

And, btw, any number xor itself inputs the number itself (1023 xor 1023=1023)

OpenStudy (inkyvoyd):

@dpaInc , having fun reading? :3

OpenStudy (anonymous):

@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

OpenStudy (anonymous):

and i think 1st one is more faster then the 2nd one

OpenStudy (anonymous):

the 2nd program will take more time in push and pop data in stack. so its slow ...

OpenStudy (inkyvoyd):

@annas , without the push and the pop, would the second be faster?

OpenStudy (anonymous):

yes

OpenStudy (inkyvoyd):

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?

OpenStudy (anonymous):

yes exactly inky ... the only purpose is to save register

OpenStudy (inkyvoyd):

Any other people's comments?

OpenStudy (anonymous):

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.

OpenStudy (inkyvoyd):

@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.

OpenStudy (anonymous):

Ah sorry. I missed a bit where the loop instruction also decrements the ecx register. Sorry for that.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!