Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Chapter 5 Exercises 5-16 A swap procedure can exchange two elements (pointed to

ID: 3674892 • Letter: C

Question

Chapter 5 Exercises

5-16 A swap procedure can exchange two elements (pointed to by ESI and EDI) of an array using

                xchg       EAX, [EDI]

                xchg       EAX, [ESI]

                xchg       EAX, [EDI]

The above code preserves the contents of the EAX register. This code requires six memory accesses. Can we do better than this in terms of the number of memory accesses if we save and restore the EAX using push and pop stack operations?

5-17 Verify that the following procedure is equivalent to the string_length procedure in section 5.10 which procedure is better and why?

                string_length1:

                                                push                      EBX

                                                sub                         AX,AX

                repeat:                                

                                                cmp                       [EBX], word 0

                                                je                            done

                                                inc                          AX

                                                inc                          EBX

                                                jmp                        repeat

                done:   

                                                pop                        EBX

                                                ret

;String length procedure         MODULE2.ASM

;        Objective: To write a procedure to compute string length of a NULL-terminated string.

;            Input: String pointer in EBX register.

;           Output: Returns string length in AX.

%include "io.mac"

               

.CODE

global string_length

string_length:

      ; all registers except AX are preserved

      push    ESI            ; save ESI

      mov     ESI,EBX        ; ESI = string pointer

repeat:

      cmp     byte [ESI],0   ; is it NULL?

      je      done           ; if so, done

      inc     ESI            ; else, move to next character

      jmp     repeat         ;       and repeat

done:

      sub     ESI,EBX        ; compute string length

      mov     AX,SI          ; return string length in AX

      pop     ESI            ; restore ESI

      ret

Explanation / Answer

5-16 A)

Yes, we can do better in terms of memory access if we save and restore the EAX using
push and pop operation. In this stack operations required only four memory access, It
can we done as given below:

Push EDI
Push ESI
Push EDI
Push ESI

The above stack operations will not use any registers and have four memory access only. so its
better to use stack. Stack operations also move the data from data segment to stack segment.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote