Question

In: Computer Science

What is the proper way to define an assembly code in in-line assembler x86 for this...

What is the proper way to define an assembly code in in-line assembler x86 for this case:

multiplication without using mul/imul, using hexadecimal numbers for "shl" instruction.

Solutions

Expert Solution

If we take a look at the timing for the multiply instruction, we will notice that the execution time for this instruction is rather long. Only the div and idiv instructions take longer on the 8086. When multiplying by a constant, we can avoid the performance penalty of the mul and imul instructions by using shifts, additions, and subtractions to perform the multiplication.

A shl operation performs the same operation as multiplying the specified operand by two. Shifting to the left two bit positions multiplies the operand by four. Shifting to the left three bit positions multiplies the operand by eight. In general, shifting an operand to the left n bits multiplies it by 2n. Any value can be multiplied by some constant using a series of shifts and adds or shifts and subtractions. For example, to multiply the ax register by ten, you need only multiply it by eight and then add in two times the original value. That is, 10*ax = 8*ax + 2*ax. The code to accomplish this is

                shl     ax, 1           ;Multiply AX by two

                mov     bx, ax          ;Save 2*AX for later

                shl     ax, 1           ;Multiply AX by four

                shl     ax, 1           ;Multiply AX by eight

                add     ax, bx          ;Add in 2*AX to get 10*AX

The ax register (or just about any register, for that matter) can be multiplied by most constant values much faster using shl than by using the mul instruction. This may seem hard to believe since it only takes two instructions to compute this product:

                mov     bx, 10

                mul     bx

However, if we look at the timings, the shift and add example above requires fewer clock cycles on most processors in the 80x86 family than the mul instruction. Of course, the code is somewhat larger (by a few bytes), but the performance improvement is usually worth it. Of course, on the later 80x86 processors, the mul instruction is quite a bit faster than the earlier processors, but the shift and add scheme is generally faster on these processors as well.

We can also use subtraction with shifts to perform a multiplication operation. Consider the following multiplication by seven:

                mov     bx, ax          ;Save AX*1

                shl     ax, 1           ;AX := AX*2

                shl     ax, 1           ;AX := AX*4

                shl    ax, 1           ;AX := AX*8

                sub     ax, bx          ;AX := AX*7

This follows directly from the fact that ax*7 = (ax*8)-ax

Thank you.


Related Solutions

In Assembly Code write an assembler code (Fibonacci.s) and show results The Fibonacci Sequence is a...
In Assembly Code write an assembler code (Fibonacci.s) and show results The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are both 1; after that, each number is the sum of the preceding two numbers. 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... For example, 1+1=2, 1+2=3, 2+3=5, 3+5=8, etc. The nth Fibonacci number is the nth number in this sequence, so for example fibonacci(1)=1, fibonacci(2)=1, fibonacci(3)=2, fibonacci(4)=3, etc....
Using x86 assembly language, create a flowchart and write an example of code that will sort...
Using x86 assembly language, create a flowchart and write an example of code that will sort 2 arrays of unsigned doubleword integers in ascending order and output the largest element in each array. Any sorting procedure can be used, but this procedure must be called twice for each array. The first time it is called, the first array should be sorted and the second time it is called, the second array must be sorted. As well as outputting which is...
X86 Assembly MASM Questions below ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;; Answer each question below by writing code at the...
X86 Assembly MASM Questions below ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;; Answer each question below by writing code at the APPROPRIATE places at the end ;;;;; of the file as indicated. ;;;;; Q2: Write the directive to bring in the IO library           ;;;;; Q3: Create a constant called DAYS_PER_WEEK and initialize it to 5 ;;;;;     Create a constant called WEEKS_PER_YEAR and initialize it to 49 ;;;;; Q4: Create a constant called DAYS_PER_YEAR by using DAYS_PER_WEEK and ;;;;;     WEEKS_PER_YEAR (of Q3) in an...
Please complete in MASM (x86 assembly language). Use the code below to get started. Use a...
Please complete in MASM (x86 assembly language). Use the code below to get started. Use a loop with indirect or indexed addressing to reverse the elements of an integer array in place. Do not copy the elements to any other array. Use the SIZEOF, TYPE, and LENGTHOF operators to make the program as flexible as possible if the array size and type should be changed in the future. .386 .model flat,stdcall .stack 4096 ExitProcess PROTO,dwExitCode:DWORD .data    ; define your...
Write an x86 assembly language program that performs equivalently to the C++ source code file shown...
Write an x86 assembly language program that performs equivalently to the C++ source code file shown below.Please note that commented out behavior must be implemented in x86 assembly language. There is no standard, portable way to perform some of these actions in C++. #include void main() { // Use registers for these in your x86 assembly language program // Only use the .data segment for string (character array) variables int eax; int esi; int ecx; int edi; // Loop the...
In assembly 1.What is the effect of the following instructions executing on a modern x86 64-bit...
In assembly 1.What is the effect of the following instructions executing on a modern x86 64-bit system (describe what happens and the final contents of the registers involved)? Can you obtain the same end result using only two instructions, and no other registers? PUSH RAX PUSH RBX PUSH RCX XOR RAX,RAX XOR RBX,RBX XOR RCX,RCX POP RAX POP RAX POP RBX 2. What is the purpose of a segment register in protected mode memory addressing? 3. For a 32-bit Pentium4...
4. Explain what is happening on each line of the following AVR assembly code. If you...
4. Explain what is happening on each line of the following AVR assembly code. If you were to execute this code what would be the final decimal values in R20, R21 and SREG registers? BCLR 0 BCLR 1 BCLR 2 BCLR 3 BCLR 4 BCLR 5 BCLR 6 BCLR 7 LDI ​R19, 0x02 LDI​R20, 0x74 LDI​R21, 0x04 LDI​R22, 0x22 ADD​R20, R22 SUB​R22, R21 ADD​R20, R21 MOV​R20, R21 JMP​DONE ADD​R21, R20 SUB​R21, R22 DONE:​SUB​R20, R21 -embedded system-
Given the below pseudocode, write the proper code that implements it using MARIE's assembly language:               ...
Given the below pseudocode, write the proper code that implements it using MARIE's assembly language:                    Input a number and store it in X; if X > 1 then    Y := X + X;    X := 0; endif; Y := Y + 1; Output the value of Y; N.B: You should include the MARIE code in your Answer, with an explanation of each instruction in your code beside it. Example:              Subt One         /Subtract 1 from AC Add a...
this there any way to shorten the line of  code. Right now it about 300 line of...
this there any way to shorten the line of  code. Right now it about 300 line of code is there anyway to shorten it to 200 or so line of code? import java.awt.*; import java.awt.Color; import java.awt.Desktop; import java.awt.Font; import java.awt.event.*; import java.net.URL; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import javax.swing.JCheckBox; import javax.swing.JColorChooser; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.KeyStroke; //MyMenuFrame will use Jframe with actionlistener...
analyze the assembly code and explain what each line is doing 000000000000063a <main>: 63a: 55 push...
analyze the assembly code and explain what each line is doing 000000000000063a <main>: 63a: 55 push ebp 63b: 48 89 e5 mov ebp,esp 63e: 48 83 ec 10 sub esp,0x10 642: c7 45 fc 00 00 00 00 mov DWORD PTR [ebp-0x4],0x0 649: eb 16 jmp 661 <main+0x27> 64b: 83 7d fc 09 cmp DWORD PTR [ebp-0x4],0x9 64f: 75 0c jne 65d <main+0x23> 651: 48 8d 3d 9c 00 00 00 lea edi,[eip+0x9c] # 6f4 <_IO_stdin_used+0x4> 658: e8 b3 fe...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT