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
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
