Lesson 08 Machine code and Compare & Test

Similar documents
Lesson 16 Analog-to-Digital Converter (ADC)

ARM Cortex-M3 Assembly Language

The ARM Architecture. With a focus on v7a and Cortex-A8

THUMB Instruction Set

Graded ARM assembly language Examples

CSE 141L Computer Architecture Lab Fall Lecture 2

Developer Suite ARM. Assembler Guide. Version 1.2. Copyright 2000, 2001 ARM Limited. All rights reserved. ARM DUI 0068B

HOMEWORK # 2 SOLUTIO

LSN 2 Computer Processors

ARM Instruction Set. ARM7TDMI-S Data Sheet. Final - Open Access

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

EECS 427 RISC PROCESSOR

High level code and machine code

The WIMP51: A Simple Processor and Visualization Tool to Introduce Undergraduates to Computer Organization

MACHINE ARCHITECTURE & LANGUAGE

CS101 Lecture 26: Low Level Programming. John Magee 30 July 2013 Some material copyright Jones and Bartlett. Overview/Questions

BCD (ASCII) Arithmetic. Where and Why is BCD used? Packed BCD, ASCII, Unpacked BCD. BCD Adjustment Instructions AAA. Example

Keil C51 Cross Compiler

CS201: Architecture and Assembly Language

2011, The McGraw-Hill Companies, Inc. Chapter 3

Programmer s Model = model of µc useful to view hardware during execution of software instructions

The 104 Duke_ACC Machine

UNIVERSITY OF CALIFORNIA, DAVIS Department of Electrical and Computer Engineering. EEC180B Lab 7: MISP Processor Design Spring 1995

Let s put together a Manual Processor

Syscall 5. Erik Jonsson School of Engineering and Computer Science. The University of Texas at Dallas

MICROPROCESSOR AND MICROCOMPUTER BASICS

Software based Finite State Machine (FSM) with general purpose processors

ARM Microprocessor and ARM-Based Microcontrollers

X86-64 Architecture Guide

Solution for Homework 2

Migrating Application Code from ARM Cortex-M4 to Cortex-M7 Processors

Binary Numbers. Binary Octal Hexadecimal

Introduction. Application Security. Reasons For Reverse Engineering. This lecture. Java Byte Code

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Instruction Set Reference

Lecture N -1- PHYS Microcontrollers

Return-oriented programming without returns

Programming Logic controllers

ADVANCED PROCESSOR ARCHITECTURES AND MEMORY ORGANISATION Lesson-17: Memory organisation, and types of memory

Instruction Set Design

M A S S A C H U S E T T S I N S T I T U T E O F T E C H N O L O G Y DEPARTMENT OF ELECTRICAL ENGINEERING AND COMPUTER SCIENCE

Lecture 3 Addressing Modes, Instruction Samples, Machine Code, Instruction Execution Cycle

Central Processing Unit (CPU)

Computer Science 281 Binary and Hexadecimal Review

ASSEMBLY LANGUAGE PROGRAMMING (6800) (R. Horvath, Introduction to Microprocessors, Chapter 6)

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

A Choices Hypervisor on the ARM architecture

Instruction Set Architecture

ARM: Assembly Language Programming

Central Processing Unit Simulation Version v2.5 (July 2005) Charles André University Nice-Sophia Antipolis

Notes on Assembly Language

Nemo 96HD/HD+ MODBUS

Summary of the MARIE Assembly Language

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

EC 362 Problem Set #2

An Introduction to the ARM 7 Architecture

Keil Debugger Tutorial

Unpacked BCD Arithmetic. BCD (ASCII) Arithmetic. Where and Why is BCD used? From the SQL Server Manual. Packed BCD, ASCII, Unpacked BCD

CPU Organization and Assembly Language

Section 29. Instruction Set

The AVR Microcontroller and C Compiler Co-Design Dr. Gaute Myklebust ATMEL Corporation ATMEL Development Center, Trondheim, Norway

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Question Bank Subject Name: EC Microprocessor & Microcontroller Year/Sem : II/IV

On Demand Loading of Code in MMUless Embedded System

l C-Programming l A real computer language l Data Representation l Everything goes down to bits and bytes l Machine representation Language

Traditional IBM Mainframe Operating Principles

Polyglot: Automatic Extraction of Protocol Message Format using Dynamic Binary Analysis

How It All Works. Other M68000 Updates. Basic Control Signals. Basic Control Signals

MACHINE INSTRUCTIONS AND PROGRAMS

Chapter 9 Computer Design Basics!

VLE 16-bit and 32-bit Instruction Length Decode Algorithm

Microprocessor and Microcontroller Architecture

Introduction to MIPS Assembly Programming

ROM Monitor. Entering the ROM Monitor APPENDIX

Digital Electronics Detailed Outline

Application Note 195. ARM11 performance monitor unit. Document number: ARM DAI 195B Issued: 15th February, 2008 Copyright ARM Limited 2007

Administration. Instruction scheduling. Modern processors. Examples. Simplified architecture model. CS 412 Introduction to Compilers

Goals. Unary Numbers. Decimal Numbers. 3,148 is s 100 s 10 s 1 s. Number Bases 1/12/2009. COMP370 Intro to Computer Architecture 1

A-level COMPUTER SCIENCE

Useful Number Systems

64-Bit NASM Notes. Invoking 64-Bit NASM

Number Representation

MICROPROCESSOR. Exclusive for IACE Students iacehyd.blogspot.in Ph: /422 Page 1

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

Chapter 7D The Java Virtual Machine

PART B QUESTIONS AND ANSWERS UNIT I

Application Note. Introduction AN2471/D 3/2003. PC Master Software Communication Protocol Specification

Caml Virtual Machine File & data formats Document version: 1.4

Computer Organization and Architecture

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

Chapter 4 Register Transfer and Microoperations. Section 4.1 Register Transfer Language

Volume Serial Numbers and Format Date/Time Verification

Machine Architecture and Number Systems. Major Computer Components. Schematic Diagram of a Computer. The CPU. The Bus. Main Memory.

Dongwoo Kim : Hyeon-jeong Lee s Husband

CS61: Systems Programing and Machine Organization

Microprocessor/Microcontroller. Introduction

SIM-PL: Software for teaching computer hardware at secondary schools in the Netherlands

ADVANCED PROCESSOR ARCHITECTURES AND MEMORY ORGANISATION Lesson-12: ARM

Assembly Language Programming

Here is a diagram of a simple computer system: (this diagram will be the one needed for exams) CPU. cache

1. Convert the following base 10 numbers into 8-bit 2 s complement notation 0, -1, -12

Transcription:

Lesson 08 Machine code and Compare & Test 1. Overview In this lesson, machine code is discussed and the compare & test instructions supported by the Cortex-M3 processor are introduced. Again, only a subset of these instructions will be discussed. The concept and syntax will generally extend to other instructions as you explore them in the Cortex-M3 instruction set. 2. Machine Code Before going any further, let s take a look at how our instructions are actually encoded As mentioned in the previous lessons, the instruction names (mnemonic) are created for users to develop assembly programs, these names are not what the processor can understand. The instructions must be translated into strings of binary machine (or object) code by an assembler such as the Keil assembler. All machine code is half-word (16 bits) aligned. It means that each instruction is either a single 16-bit half-word in that stream, or a 32-bit instruction consisting of two consecutive half-words. In the Keil IDE, the machine code is included in the listing file that is displayed in the disassembly window. In addition to the machine code, the listing also shows the memory locations where the instructions are stored and the original source code. A screenshot of the disassembly window is shown below. Memory Address Machine Code Source Code 1

The instruction encoding is explained in chapter A5 in the ARMv7-M Architecture Reference Manual document. For example, the machine code for the MOVW instruction with 16-bit constant is encoded with From ARMv7-M Architecture Reference Manual, ARM Limited, 2010. As we can see from the screenshot above, instruction movw r6, #0xC038 is encoded as 0xF24C0638. Let s look closely at how the encoding method was applied for this instruction. The constant to be loaded is 0xC038, so we can extract imm4, i, imm3, and imm8 fields as 0xC038 = 1 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 Since the destination register (Rd) is R6, the RD field in the machine code is 0110. Now, we can encode the instruction movw r6, #0xC038 as 1 1 1 1 0 0 1 0 0 1 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 1 1 1 0 0 0 The hex value for the machine code is: 0xF24C0638 (same as in example screenshot) Example: Find the machine code for movw r9, #0xA9C8. 0xA9C8 = 0b 1 0 1 0 1 0 0 1 1 1 0 0 1 0 0 0 imm4 = 1010 i = 1 imm3 = 001 imm8 = 11001000 Rd = 1001 So, the machine code for movw r9, #0xA9C8 is 11110110010010100001100111001000 The hex value for the machine code is: 0xF64A19C8 2

Example: Find the machine code for movs r4, #0x003D. Since the constant is an 8-bit number and the destination register is a lower register, we can use 16-bit version of the movs instruction. From the ARMv7-M Architecture Reference Manual document, we have Machine code: 0010010000111000 = 0x243D 3. Compare and Test The compare and test instructions are used to update the status flags in the APSR (N, Z, C, and V). From The Definitive Guide to ARM Cortex-M3 and Cortex-M4 Processors, Joseph Yiu, Elsevier, 3 nd ed, 2014. 3

It is important to note that these instructions do not alter the contents of the registers they utilize. Essentially, these instructions perform the specified operation and set the flags based on the result, but do not store the result. For example: CMP R0, R1 CMN R0, R1 TST R0, R1 TEQ R0, R1 ; Subtraction (compare) ; Calculates R0 R1 and sets the flags based on the result ; Addition (compare negative) ; Calculates R0 + R1 and sets the flags based on the result ; Bitwise AND ; Calculates R0 & R1 and sets the flags based on the result ; Bitwise XOR ; Calculates R0 ^ R1 and sets the flags based on the result Another form of these instructions can be used to operate between one register and an immediate constant value. For example: CMP R0, #0x12 TST R0, #0x12 TEQ R0, #0x12 ; Calculates R0 #0x12 and sets the flags based on the result ; Calculates R0 & #0x12 and sets the flags based on the result ; Calculates R0 ^ #0x12 and sets the flags based on the result Example: Given the initial values for R0 through R7 in the table below, complete the table on the right to show the values of the flags after the execution of the following instructions. Before Code After Register Content R0 0x12345678 R1 0x87650321 R2 0x0000f000 R3 0x12345678 R4 R5 R6 R7 CMP R0, R1 CMP R1, R0 TST R1, R2 TEQ R0, R3 TEQ R2, #0xf000 N Z C V: 0b1001 N Z C V: 0b0011 N Z C V: 0b0111 (NZ only) N Z C V: 0b0111 (NZ only) N Z C V: 0b0101 C bit is impacted because barrel shifter is used. Notes: CMP is used to compare the relative magnitude of two operands using the N bit. TST is used to see if a given bits or bit(s) are cleared. If they are, then the result is zero (Z = 1). TEQ is used to check if two operands are equal. If they are, then the result is zero (Z = 1). 4

We often combine these compare and test instructions with conditional branches, as we shall see in the next lesson. Exercise: Write an assembly program to verify the examples above. 4. References [1]. Joseph Yiu, The Definitive Guide to ARM Cortex-M3 and Cortex-M4 Processors, Elsevier, 3 rd ed, 2014. [2]. [3]. Jonathan Valvano, Introduction to ARM Cortex-M Microcontroller, 4 nd ed, 2013. ARMv7-M Architecture Reference Manual, ARM Limited, 2010. [4]. LPC17xx User manual, NXP Semiconductors, 2010. 5