Additional Assembly Programming Concepts

Slides:



Advertisements
Similar presentations
ICS312 Set 6 Operands. Basic Operand Types (1) Register Operands. An operand that refers to a register. MOV AX, BX ; moves contents of register BX to.
Advertisements

Department of Computer Science and Software Engineering
More about procedures and Video Processing. Lesson plan Review existing concepts More about procedures and boolean expression Video processing.
Video systems (continue). Practice Modify the program to get a string from a keyboard to display the input string on the middle of the screen with reverse.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#4)
Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Kip Irvine: Assembly Language for Intel-Based Computers
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#8) By Dr. Syed Noman.
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6)
Chapter 2 Software Tools and Assembly Language Syntax.
Fundamentals of Assembly language
ICS312 Set 4 Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide.MODEL SMALL.586 ;
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
Stack Operations LIFO structure (last-in,first-out) –The last value put into the stack is the first value taken out Runtime stack –A memory array that.
ZONG Wen Department of Computer Science and Engineering The Chinese University of Hong Kong
BIOS and DOS Programming in DOS INT 10 and 21H. Interrupts There are some extremely useful subroutines within BIOS or DOS that are available to the user.
Introduction to 8086 Assembly Language Assembly Language Programming University of Akron Dr. Tim Margush.
Objective At the conclusion of this chapter you will be able to:
Strings, Procedures and Macros
21/11/2005CAP2411 Input & Output Instructions CPU communicates with the peripherals through I/O registers called I/O ports. There are 2 instructions, IN.
Review of Assembly language. Recalling main concepts.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
BITS Pilani Pilani Campus Pawan Sharma Lecture /12/ EEE /INSTR/CS F241 ES C263 Microprocessor Programming and Interfacing.
File Operations. FILE PROCESSING For the purposes of the following discussion, reading means copying all or part of an existing file into memory Writing.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 3 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Program Structure Example for Data Segment CRLF EQU 0DH, 0AH PROMPT DB 'Enter a digit between 0 and 9', 0 VAR1 DB ? ARRAY DW 1234h, 23h, 0FF54h.
Azir ALIU 1 Library of common functions - emu8086 To make programming easier there are some common functions that can be included in your program To make.
Assembly language programming
Instruction set Architecture
Format of Assembly language
1st prog! Q: Read a char – from a keyboard & display it at the beginning of the next line! ====== A.
Assembly Lab 3.
COURSE OUTCOMES OF Microprocessor and programming
Assembly Language programming
Computing Fundamentals
Assembly Language Ms. V.Anitha AP/CSE SCT
Objectives Identify the built-in data types in C++
Microprocessor and Assembly Language
Microprocessor and Assembly Language
INTRODUCTION ABOUT ASSEMBLY
Computer Organization & Assembly Language
(The Stack and Procedures)
Microprocessor and Assembly Language
Defining Types of data expression Dn [name] expression Dn [name]
Symbolic Instruction and Addressing
Introduction to Assembly Language
BIC 10503: COMPUTER ARCHITECTURE
Programming 8086 – Part IV Stacks, Macros
CSCE Fall 2013 Prof. Jennifer L. Welch.
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
(Array and Addressing Modes)
(The Stack and Procedures)
Symbolic Instruction and Addressing
(Array and Addressing Modes)
Morgan Kaufmann Publishers Computer Organization and Assembly Language
INTRODUCTION ABOUT ASSEMBLY
CSCE Fall 2012 Prof. Jennifer L. Welch.
Requirements for coding in Assembly Language
Assembler Directives end label end of program, label is entry point
Chapter 6 –Symbolic Instruction and Addressing
(The Stack and Procedures)
By Nasser Halasa Assembly Language.
Introduction to 8086 Assembly Language
Procedures & Macros Introduction Syntax Difference.
(Array and Addressing Modes)
Presentation transcript:

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

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

Additional Assembly Programming Concepts

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

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

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

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

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

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

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

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, 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 BYTE PTR [BX], 44h MOV AL, VAR1 ; check value of VAR1 by moving it to AL. RET VAR1 DB 22h END ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it to AL. MOV BX, OFFSET VAR1 ; 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, VAR1 ; check value of VAR1 by moving it to AL. RET VAR1 DB 22h END

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

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

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

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

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.

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

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 100 4-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.

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…

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'

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

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

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

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

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

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

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

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

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

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

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

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.

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

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

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.

String Process Example Link to the source code

37

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 of @!. 38