More on Assembly 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.

Slides:



Advertisements
Similar presentations
Embedded Systems Programming
Advertisements

Goal: Write Programs in Assembly
1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
Optimizing ARM Assembly Computer Organization and Assembly Languages Yung-Yu Chuang with slides by Peng-Sheng Chen.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
INSTRUCTION SET ARCHITECTURES
There are two types of addressing schemes:
I/O: SPARC Assembly Department of Computer Science Georgia State University Georgia State University Updated Spring 2014.
The Assembly Language Level
Lab6 – Debug Assembly Language Lab
Binary Representation Introduction to Computer Science and Programming I Chris Schmidt.
Execution of an instruction
TK 2633 Microprocessor & Interfacing Lecture 3: Introduction to 8085 Assembly Language Programming (2) 1 Prepared By: Associate Prof. Dr Masri Ayob.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
Chapter 4 H1 Assembly Language: Part 2. Direct instruction Contains the absolute address of the memory location it accesses. ld instruction:
Topics covered: ARM Instruction Set Architecture CSE 243: Introduction to Computer Architecture and Hardware/Software Interface.
CS 300 – Lecture 6 Intro to Computer Architecture / Assembly Language Instructions.
ARM Core Architecture. Common ARM Cortex Core In the case of ARM-based microcontrollers a company named ARM Holdings designs the core and licenses it.
ARM Instructions I Prof. Taeweon Suh Computer Science Education Korea University.
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
Topic 8: Data Transfer Instructions CSE 30: Computer Organization and Systems Programming Winter 2010 Prof. Ryan Kastner Dept. of Computer Science and.
CSU0014 Assembly Languages Homepage: Textbook: Kip R. Irvine, Assembly Language for Intel-Based Computers,
Lecture 4: Advanced Instructions, Control, and Branching cont. EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer.
ASSEMBLY LANGUAGE. Assembler and Compiler Pascal A Program Compiler Version A Assembly Language Versiion A Machine Code Actual version that will be executed.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Discussion of Assignment 9 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
Lecture 2: Basic Instructions CS 2011 Fall 2014, Dr. Rozier.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Chapter 19 Number Systems. Irvine, Kip R. Assembly Language for Intel-Based Computers, Translating Languages English: Display the sum of A times.
Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Signed Integers The highest bit indicates the sign. 1 = negative, 0 = positive.
F28PL1 Programming Languages Lecture 4: Assembly Language 3.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Arrays and Strings in Assembly
Chapter 2 — Instructions: Language of the Computer — 1 Memory Operands Main memory used for composite data – Arrays, structures, dynamic data To apply.
More on Assembly 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
ARM-7 Assembly: Example Programs 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
Lecture 6: Branching CS 2011 Fall 2014, Dr. Rozier.
Home Assignment 5 Assigned. Deadline 2016 March 2, Wednesday.
Lecture 8: Loading and Storing to Memory CS 2011 Fall 2014, Dr. Rozier.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
CSC 3210 Computer Organization and Programming
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
Preliminary to Assembly Language Programming CE 140 A1/A2 28 June 2003.
Assembly Variables: Registers Unlike HLL like C or Java, assembly cannot use variables – Why not? Keep Hardware Simple Assembly Operands are registers.
F28HS Hardware-Software Interface Lecture 11: ARM Assembly Language 5.
ARM Embedded Programming Lecture 4 Accessing Memory Mapped Registers.
Arrays In high-level languages, we have several technigues available for constucting data stuctures: −One-dimensional arrays −Multi-dimensional arrays.
Lecture 10: Load/Store cont. and Integer Arithmetic CS 2011 Fall 2014, Dr. Rozier.
Writing Functions in Assembly
Morgan Kaufmann Publishers
The Cortex-M3/m4 Embedded Systems: Cortex-M3/M4 Instruction Sets
The Stack.
Additional Assembly Programming Concepts
Chapter 4 Addressing modes
Writing Functions in Assembly
The University of Adelaide, School of Computer Science
The Stack.
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
CSCI206 - Computer Organization & Programming
Topic 6: Bitwise Instructions
ARM Load/Store Instructions
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
Debugging with printf you can call printf from an ARM assembly language program some of the details will be explained later, but for now use this skeleton.
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
UCSD ECE 111 Prof. Farinaz Koushanfar Fall 2018
Optimizing ARM Assembly
Introduction to Assembly Chapter 2
Presentation transcript:

More on Assembly 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington

Pseudo-Instructions A pseudo-instruction is something that: – When you read assembly code, it looks like a regular instruction. – When it is translated to binary code, it is translated to multiple actual CPU instructions. Pseudo-instructions are handy. – We can write code that is shorter, easier to read, easier to test. However, if we care about performance: – We must always be aware of the difference between an actual instruction and a pseudo-instruction. – We should know how the pseudo-instruction is translated. 2

The LDR Pseudo-instruction ldr rd, [rn]: ldr rd, [rn, #constant] – ldr is a regular instruction, that we have been using. However, ldr can also be used as a pseudo- instruction: ldr rd, =constant. – In this usage, constant is a 32-bit number, written in decimal or hexadecimal. Example: ldr r5, =2014. – Sets r5 =

The LDR Pseudo-instruction Example: ldr r5, =2014. – Sets r5 = Why do we need this pseudo-instruction? Why can't we just write: mov r5, #2014 mov r5, constant is a real instruction, that gets translated to a single CPU instruction. For that to be possible, constant must obey certain rules (must be 8 bits, possibly shifted to the left). 4

The LDR Pseudo-instruction The ldr pseudo-instruction allows us to replace bulky code like: mov r5, #0x7f lsl r5, r5, #8 add r5, r5, #0xff lsl r5, r5, #8 add r5, r5, #0xff lsl r5, r5, #8 add r5, r5, #0xf0 with a single line: ldr r5, =7ffffff0 5

The PUSH/POP Pseudo-instructions A typical function preamble looks like this: sub sp, sp, #20 str lr, [sp, #0] str r0, [sp, #4] str r4, [sp, #8] str r5, [sp, #12] str r6, [sp, #16] This can all be replaced with this pseudo-instruction: push {r0, r4, r5, r6, lr} 6

The PUSH/POP Pseudo-instructions push {r0, r4, r5, r6, lr} The push pseudo-instruction is translated as follows: – Decrement the stack pointer as much as is needed to make room for the list of registers that is provided. – Save the provided list of registers into the stack. 7

The PUSH/POP Pseudo-instructions Similarly, a typical function wrapup looks like this: ldr lr, [sp, #0] ldr r0, [sp, #4] ldr r4, [sp, #8] ldr r5, [sp, #12] ldr r6, [sp, #16] add sp, sp, #20 bx lr This can all be replaced with this pseudo-instruction: pop {r0, r4, r5, r6} 8

The PUSH/POP Pseudo-instructions This line will produce a compiler warning: push {lr, r0, r4, r5, r6} The warning says: test1.s:20: Warning: register range not in ascending order The compiler wants you to order the list of registers in ascending order. Register lr is really register r14, so should come after the other registers in the list: push {r0, r4, r5, r6, lr} 9

Assembly Directives A directive is a line that does not specify an instruction, but provides other pieces of information. For the time being, we will cover these directives: – equ – ascii – asciz – byte – word 10

The EQU directive.equ IO_ADDRESS, 0x101f (possible other code in between) ldr r4, =IO_ADDRESS The equ directive allows us to give names to constants. This helps make the code more readable. It also helps with editing the code faster. – To change the value of the constant, we just need to change the.equ line that assigns a name to the constant. – Otherwise, we need to change the value of the constant in every single line that uses the constant. 11

The ASCII and BYTE Directive string_hello:.ascii "hello world".byte 0x00 The above lines of code define a memory location that can be referred by the rest of the code as string_hello. This memory location has the following contents: – The ASCII codes for the characters in "hello world", followed by: – A byte with content 0x00. 12

Example of Usage ldr r4, r0 := 0x 101f 1000 lrd r5, =string_hello print_str: ldrb r6,[r5] cmp '\0' = 0x00: null character? beq if yes, quit str otherwise, write character add go to next character b repeat print_done: 13

The ASCIZ Directive Strings typically have a '\0' character (ASCII code 0) at their end. Instead of having to specify manually the null character at the end of the string, we can use the.asciz directive. For example, instead of writing string_hello:.ascii "hello world".byte 0x00 We can just write: string_hello:.asciz "hello world" 14

The WORD Directive The byte directive specifies a byte of memory. The word directive specifies a word of memory. A word is 4 bytes in the ARM-7 architecture. For example, consider this line of code in C: int * array1 = {2014, 1914, 1814, 1714}; In assembly, you can write: array1:.word 2014.word 1914.word 1814.word

Negative Numbers In most modern architectures, negative numbers are represented using what is called "two's complement". Suppose that your registers hold N bits. The two's complement representation of number -X is: 2 N - X. 16

Negative Numbers: Examples The two's complement representation of number -X is: 2 N - X. In ARM-7, N = 32. How is number -1 represented: – In binary? – In hexadecimal? 17

Negative Numbers: Example The two's complement representation of number -X is: 2 N - X. In ARM-7, N = 32. How is number -1 represented: In binary: – = = In hexadecimal: – = = 0x ffff ffff If you call print_number with -1, you see 0xffffffff. 18

Input We have already seen how to print: – What we have to do is store each ASCII code that we want to print into a specific memory address: 0x101f1000 To get text input from the user, we do something similar: – We load an ASCII code from the same memory address: 0x101f1000 However, input is a little bit more complicated than output. How does the program know that we have something to print? – Easy: when the program hits an instruction that stores something to the designated address. How does the program know that the user has entered text? – Not so easy: the program hits an instruction that loads something from the designated address. – However, is there something at that address? Has the user typed something yet? 19

Input To read a character of text, we will follow a simple approach (more sophisticated/efficient approaches are available): – Wait until the user has entered a character. – When the user has entered the character, read the character. How can we know that the user has entered a character? There is a specific memory address, that holds a specific bit, that: – Is automatically set to 1 when the user enters some text. – Is automatically set to 0 when we read that text. This specific memory address is: 101f1018 The bit at position 4 in that address is set to 1 when the user has entered data. – (Bit 0 is the least significant bit). 20

Input To read a character of text, we will follow a simple approach (more sophisticated/efficient approaches are available): – Wait until the user has entered a character. – When the user has entered the character, read the character. How can we know that the user has entered a character? There is a specific memory address, that holds a specific bit, that: – Is automatically set to 1 when the user enters some text. – Is automatically set to 0 when we read that text. This specific memory address is: 101f1018 The bit at position 4 in that address is set to 1 when the user has entered data. – (Bit 0 is the least significant bit). 21