No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s1:apl:lab:lab3

This is an old revision of the document!


Programming in Assembler Laboratory

Laboratory 3

Addressing modes.

The aim of the exercise

The aim of the exercise is to learn about different addressing modes you can use to access the data in the memory.

The example code

The following code is divided into two parts. The C++ part calls the functions in a dynamically linked library. DLL contains three different functions copying the content of the source string to three destination strings using different addressing modes. One function, called CopyString_base() uses a base addressing mode. The second function CopyString_base_index() uses base + index addressing mode. The last one CopyString_base_disp() uses base + displacement.

The strings are defined inside the DLL.

C++ file

#include <iostream>
#include <windows.h>
 
typedef int(_stdcall* MyProc)();
HINSTANCE dllHandle = NULL;
 
int main()
{
    std::cout << "Hello World!\n";
    dllHandle = LoadLibrary(TEXT("JaAsm.dll"));
 
    MyProc Copy_base = (MyProc)GetProcAddress(dllHandle, "CopyString_base");
    MyProc Copy_index = (MyProc)GetProcAddress(dllHandle, "CopyString_base_index");
    MyProc Copy_disp = (MyProc)GetProcAddress(dllHandle, "CopyString_base_disp");
 
    Copy_base();
    Copy_index();
    Copy_disp();
}

Assembler dll file.

.data 
SourceDataString DB 'AGIKSZJ', 0FFH	; source string definition
DestinBaseString DB '       ', 0FFH ; buffer for destination string
DestinIndexString DB '       ', 0FFH ; buffer for destination string
DestinDisplString DB '       ', 0FFH ; buffer for destination string

.code

;**************************************************************************** 
;   This function uses base addressing mode
;**************************************************************************** 
CopyString_base PROC 
    PUSH RSI                    ; RSI, RDI are non-volatile registers
    PUSH RDI
    LEA RSI, SourceDataString   ; load address of variable 'SourceDataString' into reg. RSI
    LEA RDI, DestinBaseString   ; load address of variable 'DestinBaseString' into reg. RDI
    MOV AL, 0FFH			    ; load the ending character 'FF' into reg. AL
Copy_Loop: 
    CMP BYTE PTR [RSI], AL  	; is it the end of a string (special character FF)? 
    JE String_End			    ; found end character (guard) 
    MOV AH, [RSI]			    ; take one character from source string
    MOV [RDI], AH               ; put it into  destination string
    ADD RSI, 1			        ; increment index registers
    ADD RDI, 1
    JMP Copy_Loop			    ; jump to beginning of a loop 
String_End:	
    POP RDI
    POP RSI
    RET				            ; return form procedure
CopyString_base ENDP			; end of CopyString_base

;**************************************************************************** 
;   This function uses base + index addressing mode
;**************************************************************************** 
CopyString_base_index PROC 
    PUSH RSI                    ; RSI, RDI are non-volatile registers
    PUSH RDI
    LEA RSI, SourceDataString   ; load address of variable 'SourceDataString' into base reg. RSI
    LEA RDI, DestinIndexString  ; load address of variable 'DestinIndexString' into base reg. RDI
    MOV AL, 0FFH			    ; load the ending character 'FF' into reg. AL
    XOR RCX, RCX                ; zero index register RCX, it is volatile
Copy_Loop: 
    CMP BYTE PTR [RSI+RCX], AL  ; is it the end of a string (special character FF)? 
    JE String_End			    ; found end character (guard) 
    MOV AH, [RSI+RCX]		    ; take one character from source string
    MOV [RDI+RCX], AH           ; put it into  destination string
    ADD RCX, 1			        ; increment index register
    JMP Copy_Loop			    ; jump to beginning of a loop 
String_End:	
    POP RDI
    POP RSI
    RET				            ; return form procedure
CopyString_base_index ENDP		; end of CopyString_base_index

;**************************************************************************** 
;   This function uses base addressing mode with displacement
;**************************************************************************** 
CopyString_base_disp PROC 

; Calcucate the displacement of destination string from source
    Address_difference EQU DestinDisplString - SourceDataString

    PUSH RSI                    ; RSI, RDI are non-volatile registers
    LEA RSI, SourceDataString   ; load address of variable 'SourceDataString' into base reg. RSI
    MOV AL, 0FFH			    ; load the ending character 'FF' into reg. AL
Copy_Loop: 
    CMP BYTE PTR [RSI], AL      ; is it the end of a string (special character FF)? 
    JE String_End			    ; found end character (guard) 
    MOV AH, [RSI]		        ; take one character from source string
    MOV [RSI+Address_difference], AH   ; put it into destination string
    ADD RSI, 1			        ; increment index register
    JMP Copy_Loop			    ; jump to beginning of a loop 
String_End:	
    POP RSI
    RET				            ; return form procedure
CopyString_base_disp ENDP	    ; end of CopyString_base_disp

;**************************************************************************** 

END

*def file*

LIBRARY JaAsm
EXPORTS CopyString_base
EXPORTS CopyString_base_index
EXPORTS CopyString_base_disp

Tasks to be done during laboratory

  1. Compile the example code and observe its behaviour. Set the breakpoint at the beginning of the code and see how the contents of the registers change with every loop pass.
  2. Make the function more universal: define the string in a C++ file and pass its address as the parameter to the function. Pass the character as the second parameter.
  3. Rewrite the search algorithm with vector instructions (SSE, or AVX). Suggest what to do if the string length is not a multiple of register size.
  4. Compare execution time for scalar (normal) instructions and vector instruction for the same string.
en/edu/zmitac/s1/apl/lab/lab3.1733221302.txt.gz · Last modified: by ktokarz