## A community for students. Sign up today

Here's the question you clicked on:

## inkyvoyd 3 years ago 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.

• This Question is Closed
1. 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.

2. inkyvoyd

*I mean, dword

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

4. inkyvoyd

I'm using MASM as my assembler.

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

6. inkyvoyd

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

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

8. inkyvoyd

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

9. inkyvoyd

@farmdawgnation @annas

10. inkyvoyd

@imranmeah91 @nbouscal

11. inkyvoyd

@cshalvey

12. inkyvoyd

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

13. inkyvoyd

@dpaInc , having fun reading? :3

14. annas

@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

15. annas

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

16. annas

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

17. inkyvoyd

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

18. annas

yes

19. 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?

20. annas

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

21. inkyvoyd

Any other people's comments?

22. slotema

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.

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

24. slotema

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

#### Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy