I am doing some exercises in assembly language and I found a question about optimization which I can’t figure out. Can anyone help me with them
So the question is to optimize the following assembly code:
—————————-Example1————————-
mov dx, 0 ---> this one I know-> xor dx,dx
—————————-Example2————————
cmp ax, 0
je label
—————————-Example3————————-
mov ax, x
cwd
mov si, 16
idiv si
—-> Most I can think of in this example is to subs last 2 lines by idiv 16, but I am not sure
—————————-Example4————————-
mov ax, x
mov bx, 7
mul bx
mov t, ax
—————————-Example5—————————
mov si, offset array1
mov di, offset array2
; for i = 0; i < n; ++i
do:
mov bx, [si]
mov [di], bx
add si, 2
add di, 2
loop do
endforloop
For example 2, you should look at the
andortestopcodes. Similar to example 1, they allow you to remove the need for a constant.For example 4, remember that
x * 7is the same asx * (8 - 1)or, expanding that,x * 8 - x. Multiplying by eight can be done with a shift instruction.For example 5, you’d think Intel would have provided a much simpler way to transfer from SI to DI, since that is the whole reason for their existence. Maybe something like a REPetitive MOVe String Word 🙂