Presentation is loading. Please wait.

Presentation is loading. Please wait.

Additional Assembly Programming Concepts

Similar presentations


Presentation on theme: "Additional Assembly Programming Concepts"— Presentation transcript:

1 Additional Assembly Programming Concepts
Computer Architecture SYST27198 Additional Assembly Programming Concepts Prof. Rachel Jiang Winter 2017 Sheridan College

2 Acknowledgement These lecture slides are partly based on material by Georg Feil, John Wang and Doug Waechter Media tagged ‘CC’ is under Creative Commons license Additional sources are cited separately

3 Additional Assembly Programming Concepts

4 Topics Variables (data) Constants Getting the Address of a Variable
Library of Common Functions Processing strings

5 More Assembly Programming
The 8086 assembler supports these additional assembly programming features: Variables (data) Constants Getting the Address of a Variable … Using these can make assembly programs more like a high-level language 4

6 Variables Our 8086 assembler supports two types of variables: Byte, DB and Word, DW 5

7 Variables Syntax for variable declaration: name DB value name DW value
DB is “Define Byte” & DW is “Define Word” name can be any letter or digit combination (starting with a letter) value can be any numeric value, or the “?” symbol for not initialized variable ORG 100h MOV AL, var1 MOV BX, var2 RET ; stop (return to OS) var1 DB 7 var2 DW 1234h 6

8 Variables: Example Please emulate: ORG 100h MOV AL, var1 MOV BX, var2
RET ; stop (return to OS) var1 DB 7 ;? For uninitialized var2 DW 1234h Note: the assembler will replace all variable names with indirect memory references (square brackets) Be careful never to execute data defined by DB or DW 7

9 Variables: Example Notice the disassembled code
This means you’ll access the value of the variable, not its memory address (location) 8

10 Getting the Address of a Variable
8086 instruction: LEA – Load Effective Address Syntax: LEA register, memory The address of memory (offset) will be loaded to the register Both OFFSET and LEA can be used to get the offset address of the variable. 9

11 Getting the Address of a Variable
mov ax, [bx] ; Move the 2 bytes in memory at the address contained in BX into AX mov [var], bx ; Move the contents of BX into the 2 bytes at memory address var. ORG 100h MOV AL, VAR ; check value of VAR1 by moving it to AL. LEA BX, VAR ; get address of VAR1 in BX. MOV [BX], 44h ; modify the contents of VAR1. ;MOV BYTE PTR [BX], 44h MOV AL, VAR ; check value of VAR1 by moving it to AL. RET VAR1 DB 22h END ORG 100h MOV AL, VAR ; check value of VAR1 by moving it to AL. MOV BX, OFFSET VAR ; get address of VAR1 in BX. MOV [BX], 44h ; modify the contents of VAR1. ;MOV BYTE PTR [BX], 44h ; modify the contents of VAR1. MOV AL, VAR ; check value of VAR1 by moving it to AL. RET VAR1 DB 22h END

12 Load Effective Address Example
ORG 100h LEA AX, var1 ; need AX, not AL LEA BX, var2 MOV [BX], 0ABCDh RET var1 DB 7 var2 DW 1234h 11

13 Common mistakes ORG 100h MOV AX, var1 MOV BL, var2 RET var1 DB 7 var2 DW 1234h ORG 100h MOV AL, VAR LEA BL, VAR ; get address of VAR1 in BL. MOV BYTE PTR [BH], 44h MOV AL, VAR ;. RET VAR1 DB 22h END

14 Getting the Address of a Variable
LEA can be used to pass the address of a variable to a procedure Using the variable directly would pass the value of the variable For example, you can use this to pass large amounts of data to a procedure This can also be used to return data from a procedure Like pointers in C, or references in C# or Java 13

15 Address of Variables: Example 2
Please emulate: ORG 100h MOV AL, var1 ; check value of VAR1 by moving it to AL LEA BX, var1 ; get address of VAR1 in BX MOV [BX],44h ; modify the contents of VAR1 MOV AL, var1 RET var1 DB 22h END 14

16 Size Directives mov BYTE PTR [bx], 2 ; Move 2 into the single byte at the address stored in BX. mov WORD PTR [bx], 2 ; Move the 16-bit integer representation of 2 into the 2 bytes starting at the address in EBX. mov DWORD PTR [bx], 2     ; Move the 16-bit integer representation of 2 into the 2 bytes starting at the address in BX.

17 Exercise Are the following valid variable declarations? var DB 64 var2
DW ? Y DD 30000    

18 String Examples Z DD 1, 2, 3 ; Declare three 4-byte values, initialized to 1, 2, and 3. The value of location Z + 8 will be 3. bytes   DB 10 DUP(?) ; Declare 10 uninitialized bytes starting at location bytes. arr DD 100 DUP(0)     ; Declare byte words starting at location arr, all initialized to 0 str DB 'hello',0 ; Declare 6 bytes starting at the address str, initialized to the ASCII character values for hello and the null (0) byte.

19 Exercise Write an assembly language program that will create two variables called varOne and varTwo of Byte type and initialize with some values… Your program will add the two values in those variable and save the result in a third variable called Result, please use LEA to get the address of the Result variable in memory… Modify the program to change the variables type from DB to DW…

20 Array processing ORG 100h mov ah, 0Eh LEA BX, str mov cx, 5
rep: MOV al, [BX] int 10h inc Bx loop rep RET str DB 'Hello'

21

22 Constants The EQU statement defines a constant:
name EQU <any expression> Constants are like variables, but they exist only while your program is being assembled EQU is an assembler directive, does not generate machine code Value of a constant cannot be changed 21

23 Constants: Example k EQU 5 MOV AX, k and MOV AX, 5 are functionally identical 22

24 Example ORG 100h MOV AX, varOne mov BX, varTwo add AX, BX mov BX, YEAR
MUL BX LEA BX, [Result] MOV word ptr [BX], AX MOV word ptr [BX+2], DX RET YEAR equ 2015 varOne DW 25h varTwo DW 43h Result DD ? END

25 Review: Procedures RET PROC and ENDP are assembler directives
Return from procedure Return control to the OS PROC and ENDP are assembler directives Don’t actually generate machine code Use the CALL instruction to call a procedure, followed by the name 24

26 Macros A macro is a group of assembly instructions that gets copied (as if it was “pasted”) every time you write its name Macros work a bit like procedures, but they exist only while your code is being assembled After assembling all macro “calls” are replaced with their instructions Macros support parameters by direct substitution A declared macro that’s not used in the code will be ignored by the assembler 25

27 Macros vs. Procedures Calling Macros vs. Procedures
Procedure needs the CALL instruction Macro only needs its name to be typed Procedure is located in a specific memory address The CPU will jump to this memory address when the procedure is called Will not significantly increase executable file size Macro is expanded directly in the code each time it’s used May increase executable file size significantly 26

28 Macro Example It’s a good idea to put macro declarations in a separate file 27

29 Library of Common Functions
Emu8086 library of common functions is in emu8086.inc These functions can be macros or procedures To use the library put the following line in the beginning of your source file: include 'emu8086.inc' You can find it in folder C:\emu8086\inc (There’s documentation inside! ) 28

30 Library of Common Functions - emu8086.inc
a good example of how macros can be used, this file contains several macros to make coding easier for you Unlike procedures, macros should be defined above the code that uses it Macro MyMacro MACRO p1, p2, p3 MOV AX, p1 MOV BX, p2 MOV CX, p3 ENDM ORG 100h MyMacro 1, 2, 3 MyMacro 4, 5, DX RET Compiled Code MOV AX, 00001h MOV BX, 00002h MOV CX, 00003h MOV AX, 00004h MOV BX, 00005h MOV CX, DX

31 Library of Common Functions
There are various useful macros and procedures in the library Screen output Cursor control Keyboard input (e.g. SCAN_NUM ) See the documentation (comments) in the file for details 30

32 Library of Common Functions
The macro name, or CALL the procedure, as required To use a procedure like PRINT_STRING you need to put DEFINE_PRINT_STRING near the end of your program Can also go at the start, after the ORG Note: The define is actually a macro that defines the procedure using PROC! 31

33 Common Functions by Macro
If you plan to use your macros in several programs, it may be a good idea to place all macros in a separate file. Place that file in Inc folder and use INCLUDE file- name directive to use macros. See Library of common functions - emu8086.inc for an example of such file.

34 Emu8086 Library Example include "emu8086.inc" org 100h PRINTN "Enter first integer" call SCAN_NUM ;get first integer PRINTN ;new line mov ax,cx PRINTN "Enter second integer" call SCAN_NUM PRINTN ;new line add ax,cx PRINTN "The sum is" call PRINT_NUM ; print contents of ax ret DEFINE_SCAN_NUM DEFINE_PRINT_NUM DEFINE_PRINT_NUM_UNS end 33

35 More Emu8086 Library Examples
See example programs in SLATE PRINTN macro PRINT_STRING procedure GET_STRING procedure Note the PRINTN macro can’t be used to print memory buffers (e.g. DB) 34

36 GET_STRING -procedure to get a null terminated string from a user, the received string is written to buffer at DS:DI, buffer size should be in DX. Procedure stops the input when 'Enter' is pressed. To use it declare: DEFINE_GET_STRING before END directive.

37 String Process Example
Link to the source code

38 37

39 Programming Exercises
Write an assembler program using Emu8086 that inputs a string of lower case letters and symbols from the keyboard and then displays the equivalent UPPER case letters, removes the symbols, and add a symbol of *, ? In between each letter. Modify your program so the input string can contain a combination of UPPER and lower case letters and the output will be the string in reverse case with the symbol 38

40


Download ppt "Additional Assembly Programming Concepts"

Similar presentations


Ads by Google