1

I am a beginner for assembly and I am writing a code in assembly to compare with python then see if it's same.

mov eax, x        ; x
mov ebx, y        ; y 
mov ecx, z        ; z
loop:             ; loop 
    inc eax       ; plus 1 to x
    cmp eax,ecx   ; compare x and z
    jne add_1     ; if not equal then jump to add_1
    je done       ; if equal then jump to done
add_1:            ; add_1 statement
  add ebx, 1      ; y=y+1
  jmp cmp_x_and_z ; jump back to loop
done: 

and I want to compare with this in python

for x in range(z):
    y = y + 1

Is my logical right or there is anything than I can change to make it better?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
djflvv233
  • 25
  • 6
  • 3
    What do you mean by _"see if it's same"_? Python compiles to bytecode, not machine code, so there is no way to compare them byte by byte. – Selcuk Nov 14 '19 at 03:40
  • 1
    “See if it’s the same.” You’re going to need to be more specific, that’s an incredibly broad question. – AMC Nov 14 '19 at 03:54
  • Python integers are arbitrary precision, not fixed-width with wrapping at 32 bits. But sure, this is one inefficient way to implement a similar algorithm with 32-bit integers in asm. – Peter Cordes Nov 14 '19 at 04:27

1 Answers1

2

You have the right idea, so well done!

  1. The jump label target cmp_x_and_z should be loop, just a typo , I think.

  2. x in eax should be initialized to 0 not xx is a loop local variable, not a parameter (so x=x doesn't make sense but x=0 here does).

  3. While we want the cmp and following branch to be at the top of the loop as you have it (so it acts like a while and doesn't execute the body even once if out of bounds), the inc should be delayed until past the body of the loop though within the section that executes during backwards branching to form the loop.  By the definition of most languages, we want the loop iteration variable to have the value pre-increment during the body (so we can do a[x] and such).


    mov eax, 0    ; x   ... =0 (not x=x)
    mov ebx, y    ; y  (y is an unknown input value)
    mov ecx, z    ; z  (z is the end point)
loop:             ; loop 
    cmp eax,ecx   ; compare x and z
    jne add_1     ; if not equal then jump to add_1
    je done       ; if equal then jump to done
add_1:            ; add_1 statement
    add ebx, 1    ; y=y+1
    inc eax       ; increment of x++ done after loop body
    jmp loop      ; jump back to loop
done: 

If you want to improve the (conditional) branch over (unconditional) branch, then reverse the condition test:

    mov eax, 0    ; x   ... =0 (not x=x)
    mov ebx, y    ; y  (y is an unknown input value)
    mov ecx, z    ; z  (z is the end point)
loop:             ; loop 
    cmp eax,ecx   ; compare x and z
    je  done      ; if not equal then jump to add_1
    add ebx, 1    ; y=y+1
    inc eax       ; increment of x++ done after loop body
    jmp loop      ; jump back to loop
done: 

It never makes sense to write je and jne back to back. They're opposite conditions so the second one is always taken if reached at all. If both paths of execution need to go somewhere else, use jmp for the second one. If not, use a single conditional branch that goes somewhere else or falls through. Fall-through is the most efficient case for branches.

You could also rearrange this loop to avoid the jmp at the bottom, with the conditional branch as the loop branch. Why are loops always compiled into "do...while" style (tail jump)?

Or if you actually wanted to optimize, you'd do y += z instead of looping to do z increments by 1.


There are several other improvements possible, but that is probably sufficient for this question.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Erik Eidt
  • 23,049
  • 2
  • 29
  • 53