0

enter image description hereI'm new to assembly and I don't understand why the emulator is skipping line 6 and not increment at line 7

Please explain to me I'm so lost in this:

org 100h

x db 1, 2, 3, 3, 5

mov SI, 0 ;index

mov AL, 0       ;max
mov AL, x[SI]       ;assign max

mov SI, 1   ;initialize SI to 1

next:

cmp x[SI],AL

jg adding 

inc SI

cmp SI,4

JNE next

hlt 

adding:
mov AL,x[si]

inc SI

jmp next

here is an image, Al is suppose to be intitizailzed as x[0], which means its suppose to be 1, but i can see the value is 8B

enter image description here

2 Answers2

2

There are a number of issues here:

  • The initial mov al,0 is not needed as you read the first value on the array in next.
  • The "adding: code has no termination condition - it will read past the end of x You should share the end of array code between both parts.
  • Even if your end of array check worked, it should be using 5, not 4. Consider using a label subtract or something to get this value from the code though.

Here are some suggestions:

org 100h 
mov SI, 0 ;index    ; Set index to 0

mov AL, [x+SI]       ; Assign max
inc SI              ; Next index

next:
cmp [x+SI],AL        ; Compare next array element
jg adding           ; If Greater, jump to adding

check_end:        
inc SI              ; Increment array index and check for end
cmp SI,5            ; Check is after increment, so it needs to be 5

jne next
hlt                 ; If done (SI == 5), halt

adding:
mov AL,[x+si]        ; Update AL with new max
jmp check_end       ; Jump to condition check

x db 1, 2, 3, 3, 5
Michael Dorgan
  • 12,285
  • 2
  • 29
  • 61
  • Thank you so much for your reply! im using an emulator, when im stepping over "mov AL, x[SI] ; Assign max " AL is not changing, why is that? – Itamar Nahum Mar 04 '21 at 00:50
  • Like Basically these two are skipped over and i don't understand why:mov AL, x[SI] ; Assign max inc SI ; Next index – Itamar Nahum Mar 04 '21 at 01:09
  • Basically we can run the progrram without these two lines:, org 100h x db 1, 2, 3, 3, 5 mov SI, 0 ;index ; Set index to 0 next: cmp x[SI],AL ; Compare next array element jg adding ; If Greater, jump to adding check_end: inc SI ; Increment array index and check for end cmp SI,5 ; Check is after increment, so it needs to be 5 jne next hlt ; If done (SI == 5), halt adding: mov AL,x[si] ; Update AL with new max jmp check_end ; Jump to condition chec – Itamar Nahum Mar 04 '21 at 01:12
  • I just moved the base address into the `[]` above. See if that fixes it for your assembler. – Michael Dorgan Mar 04 '21 at 18:52
  • https://i.stack.imgur.com/nc5Zv.png please review this image of the compiler, for some reason, AL does not get x[0] as max value at the beginning,it stills works but i dont understand why – Itamar Nahum Mar 05 '21 at 19:38
  • There is a chance that your emulator is being really dumb and displaying bad instructions for the first few bytes because of the db array. Try adding 4 or so 0s to your x array to see if that allows the debugger to show the data correctly. You could also try placing your x array at the end of the code so that it can show better as then it isn't trying to display code and data as code at the same time. – Michael Dorgan Mar 05 '21 at 20:49
  • 1
    @ItamarNahum: You put your data in the path of execution. IIRC, `add [bp+si], ax` looks right for disassembly of `01 02`. – Peter Cordes Mar 05 '21 at 21:18
  • 1
    Ahhh - that is exactly right. So putting the data at the end would have fixed it :) – Michael Dorgan Mar 05 '21 at 22:53
  • @PeterCordes you mean to declare the x array at the end of the code? if so its still not working please review this image-https://i.stack.imgur.com/nc5Zv.png – Itamar Nahum Mar 06 '21 at 14:21
  • @ItamarNahum: Yes, like in the duplicate, and like in Michael's update to this answer. Your image still shows `x db ...` at the start, causing exactly the same problem. IDK what you mean "if so", because you didn't change that. x86 machine code is a byte stream that's not self-synchronizing; your bytes of data get the CPU out of sync with the instruction boundaries in your source code; that's why decoding doesn't line up with the source for a while after the array. – Peter Cordes Mar 06 '21 at 23:45
-1

Basically, we can run the program without these two lines since they are being skipped by the emulator for some reason:

mov AL, x[SI] ; Assign max

inc SI ; Next index

org 100h

x db 1, 2, 3, 3, 5

mov SI, 0 ;index ; Set index to 0

next: cmp x[SI],AL ; Compare next array element jg adding ; If Greater, jump to adding

check_end:
inc SI ; Increment array index and check for end

cmp SI,5 ; Check is after increment, so it needs to be 5

jne next

hlt ; If done (SI == 5), halt

adding:

mov AL,x[si] ; Update AL with new max

jmp check_end ; Jump to condition chec

  • Are you saying if you removed those lines from your source code, the program would still work? That doesn't make sense, nothing else is updating AL to the currently-found max. Are you sure you saved your source and re-assembled before running in the debugger? Like maybe you're debugging a version of the program that doesn't match the source. – Peter Cordes Mar 04 '21 at 04:25
  • If you want more help figuring out what's going on, update your question (not this answer) with more details like which emulator/simulator you're using, and what exact sequence of instructions it shows being executed for a loop iteration. – Peter Cordes Mar 04 '21 at 04:29