How To Write An Array In A Microsoft Zil

Similar documents
MIPS Assembly Code Layout

Typy danych. Data types: Literals:

Introduction to MIPS Assembly Programming

Translating C code to MIPS

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

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Lab Work 2. MIPS assembly and introduction to PCSpim

Reduced Instruction Set Computer (RISC)

CmpSci 187: Programming with Data Structures Spring 2015

5 MIPS Assembly Language

Sources: On the Web: Slides will be available on:

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

PROBLEMS (Cap. 4 - Istruzioni macchina)

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

5 Arrays and Pointers

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

We will use the accumulator machine architecture to demonstrate pass1 and pass2.

Instruction Set Architecture. or How to talk to computers if you aren t in Star Trek

Informatica e Sistemi in Tempo Reale

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Figure 1: Graphical example of a mergesort 1.

Answers to Review Questions Chapter 7

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

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

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

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Stacks. Linear data structures

Section 1.4 Place Value Systems of Numeration in Other Bases

Illustration 1: Diagram of program function and data flow

LC-3 Assembly Language

Pemrograman Dasar. Basic Elements Of Java

The C Programming Language course syllabus associate level

Byte Ordering of Multibyte Data Items

Conditionals (with solutions)

Efficient Low-Level Software Development for the i.mx Platform

Passing 1D arrays to functions.

An overview of FAT12

Jorix kernel: real-time scheduling

Chapter 7 Assembly Language

So far we have considered only numeric processing, i.e. processing of numeric data represented

Base Conversion written by Cathy Saxton

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Chapter 2: Elements of Java

Winter 2002 MID-SESSION TEST Friday, March 1 6:30 to 8:00pm

arrays C Programming Language - Arrays

Lecture 11 Array of Linked Lists

Instruction Set Architecture

Instruction Set Architecture (ISA)

Common Data Structures

Arrays. Introduction. Chapter 7

MIPS Assembler and Simulator

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

MACHINE INSTRUCTIONS AND PROGRAMS

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

Lesson 10: Video-Out Interface

6.080/6.089 GITCS Feb 12, Lecture 3

The Linux Virtual Filesystem

Chapter 2 Topics. 2.1 Classification of Computers & Instructions 2.2 Classes of Instruction Sets 2.3 Informal Description of Simple RISC Computer, SRC

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Chapter 4: Computer Codes

JavaScript: Control Statements I

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

public static void main(string[] args) { System.out.println("hello, world"); } }

Java Interview Questions and Answers

What to do when I have a load/store instruction?

Tutorial on C Language Programming

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Writing Control Structures

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

How To Write Portable Programs In C

MBP_MSTR: Modbus Plus Master 12

VB.NET Programming Fundamentals

ENTTEC Pixie Driver API Specification

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

I PUC - Computer Science. Practical s Syllabus. Contents

Repetition Using the End of File Condition

Implementation Aspects of OO-Languages

In this Chapter you ll learn:

MACHINE ARCHITECTURE & LANGUAGE

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

Memory Systems. Static Random Access Memory (SRAM) Cell

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Data Structures using OOP C++ Lecture 1

Introduction to Java

Variables, Constants, and Data Types

Principles of Database Management Systems. Overview. Principles of Data Layout. Topic for today. "Executive Summary": here.

1 Classical Universal Computer 3

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

More MIPS: Recursion. Computer Science 104 Lecture 9

COMP 303 MIPS Processor Design Project 4: MIPS Processor Due Date: 11 December :59

Chapter 7D The Java Virtual Machine

Static Code Analysis Procedures in the Development Cycle

This section describes how LabVIEW stores data in memory for controls, indicators, wires, and other objects.

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

Using C to Access Data Stored in Program Memory on the TMS320C54x DSP

Number Representation

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Transcription:

Array Declaration and Storage Allocation The first step is to reserve sufficient space for the array: 1.data list:.space 1000 # reserves a block of 1000 bytes This yields a contiguous block of bytes of the specified size. The label is a symbolic name for the address of the beginning of the array. list == 1004000 1004000 1004001 1004002 1004003 1004004 Memory allocation for list The size of the array is specified in bytes could be used as: 1004999 array of 1000 char values (ASCII codes) array of 250 int values array of 125 double values There is no sense in which the size of the array is "known" by the array itself.

Array Declaration with Initialization An array can also be declared with a list of initializers: 2.data vowels:.byte 'a', 'e', 'i', 'o', 'u' pow2:.word 1, 2, 4, 8, 16, 32, 64, 128 Memory vowels names a contiguous block of 5 bytes, set to store the given values; each value is stored in a single byte. Address of vowels[k] == vowels + k 1004000 1004001 1004002 1004003 1004004 1004005 97 101 105 111 117 alloc for vowels 1004006 1004007 1004008 pow2 names a contiguous block of 32 bytes, set to store the given values; each value is stored in a word (4 bytes) Address of pow2[k] == pow2 + 4 * k 1004009 1004010 1004011 1004012 1 2 alloc for pow2

Another View 3 Viewed as hex nybbles, the contents of memory would look like (in little-endian): 61 65 69 6F 75 01 00 00 00 02 00 00 00 10040000 10040001 10040002 10040008 10040012 0110 0001 Note that endian-ness affects the ordering of bytes, not the ordering of the nybbles within a byte.

Array Traversal and Initialization Here's an array traversal to initialize a list of integer values: 4.data list:.space 1000 listsz:.word 250 # using as array of integers.text main: lw la li $s0, listsz # $s0 = array dimension $s1, list # $s1 = array address $t0, 0 # $t0 = # elems init'd initlp: beq $t0, $s0, initdn sw $s1, ($s1) # list[i] = addr of list[i] addi $s1, $s1, 4 # step to next array cell addi $t0, $t0, 1 # count elem just init'd b initlp initdn: li $v0, 10 QTP: why 4?

Array Traversal Details 5 1004008 initlp: beq $t0, $s0, initdn 1004008 sw $s1, ($s1) 1004012 addi $s1, $s1, 4 1004012 addi $t0, $t0, 1 1004016 initdn: b initlp 1004016 1004020 A variable that stores an address is called a pointer. 1004020 Here, $s1 is a pointer to a cell of the array list. We can re-target $s1 to a different cell by adding an appropriate value to it. 1004024 1004024

Alternate Traversal Logic This traversal uses pointer logic to terminate the loop: 1004008 6.data list:.space 1000 listsz:.word 250.text main: la lw addi sll add $s1, list $s0, listsz $s0, $s0, -1 # index of last cell $s0, $s0, 2 # offset of last cell $s0, $s0, $s1 # ptr to last cell initlp: bgt $s1, $s0, initdn sw $s1, ($s1) addi $s1, $s1, 4 b initlp initdn: li $v0, 10 1004012 1004016 1004996 1004008 1004012 1004016 1004024 QTP: rewrite this using the do-while pattern shown in the previous lecture 1005000

Array Bounds Issues 7 An array can also be declared with a list of initializers: Memory.data vowels:.byte 'a', 'e', 'i', 'o', 'u' pow2:.word 1, 2, 4, 8, 16, 32, 64, 128 What happens if you access an array with a logically-invalid array index? vowels[5]?? contents of address 1004005 While vowels[5] does not exist logically as part of the array, it does specify a physical location in memory. 1004004 1004005 1004006 1004007 1004008 1004012 117 1 2 alloc for vowels alloc for pow2 What is actually stored there is, in general, unpredictable. In any case, the value is not one that we want 1004036

Special Case: Array of Characters As we've seen, the declaration:.data vowels:.byte 'a', 'e', 'i', 'o', 'u' 8 Leads to the allocation: 61 65 69 6F 75 However, the declaration:.data vowels:.asciiz "aeiou" Leads to the allocation: 61 65 69 6F 75 00 An extra byte is allocated and initialized to store 0x00, which acts as a marker for the end of the character sequence (i.e., string). This allows us to write loops to process character strings without knowing the length of the string in advance.

Example: Searching a Character String 9.data char:.byte 'u' vowels:.asciiz "aeiou" main:.text lb li la lb $t0, char # load character to look for $t1, 0 # it's not found yet $s0, vowels # set pointer to vowels[0] $s1, ($s0) # get vowels[0] srchlp: beq $s1, $zero, srchdn # check for terminator seq $t1, $s1, $t0 # compare characters bgt $t1, $zero, srchdn # check if found addi $s0, $s0, 1 # no, step to next vowel lb $s1, ($s0) # load next vowel b srchlp srchdn: li $v0, 10

Example: Setup Details 10 lb li la lb $t0, char # load character to look for $t1, 0 # it's not found yet $s0, vowels # set pointer to vowels[0] $s1, ($s0) # get vowels[0] $t0 00 00 00 75 char vowels 75 61 65 69 6F 75 00 $t1 00 00 00 00 $s0 $s1 00 00 00 61

Example: Loop Details 11 srchlp: beq $s1, $zero, srchdn # string terminator is 0x00 seq $t1, $s1, $t0 # $t1 = 1 iff $s1 == $t0 srchdn: bgt addi lb b $t1, $zero, srchdn # if match found, exit loop $s0, $s0, 1 # step to next elem of vowels $s1, ($s0) # load next elem of vowels srchlp

Example: Print Array Contents 12.data list:.word 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 size:.word 10 lw $t3, size la $t1, list # get array address li $t2, 0 # set loop counter prnlp: beq $t2, $t3, prndn # check for array end lw $a0, ($t1) # print list element li $v0, 1 la $a0, NL # print a newline li $v0, 4 prndn: addi addi b $t2, $t2, 1 # advance loop counter $t1, $t1, 4 # advance array pointer prnlp # repeat the loop

Example: Details 13 # #1 prints and integer to stdout lw $a0, ($t1) # takes value via register $a0 li $v0, 1 # takes # via register $v0 # #4 prints asciiz to stdout la $a0, NL # takes address of string via $a0 li $v0, 4 # takes # via register $v0

Example: Palindromes 14 A palindrome is a sequence of characters that reads the same from left to right as from right to left: able was i ere i saw elba anna madam It is generally permitted to adjust capitalization, spaces and punctuation: A man, a plan, a canal, Panama! Madam, I'm Adam. For the purpose of an example, we will not allow such manipulations.

Example: Reading a String 15 We must reserve space to store the characters: buffer:.space 1025 # 1024 maximum, plus a terminator We'll want to issue a prompt to the user to enter the string to be tested: user_prompt:.asciiz "Enter... of no more than 1024 characters.\n" We can use a couple of system calls to get the input: main: ## Prompt the user to enter a string: la $a0, user_prompt li $v0, 4 ## Read the string, plus a terminator, into the buffer la $a0, buffer li $a1, 1024 li $v0, 8

Example: Finding the End of the String We must locate the end of the string that the user entered: 16 la la LengthLp: lb beqz addi b LengthDn: addi $t1, buffer # lower array pointer = array base $t2, buffer # start upper pointer at beginning $t3, ($t2) # grab the character at upper ptr $t3, LengthDn # if $t3 == 0, we're at the terminator $t2, $t2, 1 # count the character LengthLp # repeat the loop $t2, $t2, -2 # move upper pointer back to last char QTP: why -2?

Example: Testing the String 17 Now we'll walk the pointers toward the middle of the string, comparing characters as we go: TestLp: bge $t1, $t2, Yes # if lower pointer >= upper pointer, yes lb lb bne addi subi b $t3, ($t1) # grab the character at lower ptr $t4, ($t2) # grab the character at upper pointer $t3, $t4, No # if different, it's not a palindrome $t1, $t1, 1 # increment lower ptr $t2, $t2, 1 # decrement upper ptr TestLp # restart the loop

Example: Reporting Results 18 Yes: la $a0, is_palindrome_msg # print confirmation li $v0, 4 b exit No: la $a0, is_not_palindrome_msg # print denial li $v0, 4