Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assembly Language Programming in jasmin

Similar presentations


Presentation on theme: "Assembly Language Programming in jasmin"— Presentation transcript:

1 Assembly Language Programming in jasmin
Chapter 3 Assembly Language Programming in jasmin

2 3.1 Java, the Programming System
The design of Java, the language, has strongly influenced the designs both of the Java Virtual Machine and the assemblers for that machine. Java strongly supports and encourages object-oriented programming. Focuses on objects each with their own set of actions that they can perform or that can be performed up them.

3 3.1 Java, the Programming System
Objects can be grouped into classes of similarly-typed objects. Example: “cars” steering wheels, gas pedals, brakes, headlights. Turn left or right Slow down or stop Java forces all functions to be attached to classes all executable program code to be stored as separate (and separable) “class files”.

4 3.1 Java, the Programming System
Java class files are portable between different machine types. Written in the machine language of the Java Virtual Machine. Any class file compiled on any machine, can be freely copied to any other machine and will still run. JVM bytecode will only run on a copy of the JVM, but the JVM is enabled on almost every hardware platform.

5 3.1 Java, the Programming System
When a bytecode is executed the computer runs a special program to load the class from disk into the computer's memory. Loads other classes needed to support the class Verifies the structural soundness and safety of the class and its methods Initializes static and class-level variables to the appropriate values. These steps are all a built-in part of the JVM implementation.

6 3.1 Java, the Programming System
Running Java programs Compile or convert code into a class file. Create a (software) instance of the Java Virtual Machine in order to execute it. Looks for a file named TheClassOfInterest.class and then proceeds to load, verify, and initialize it. Looks for a specific method in that class called main(), invokes that method.

7 3.1 Java, the Programming System
Any standalone Java application class must contain a “main” method. On many systems just clicking on the icon corresponding to the .class file will launch a JVM and run the class file. Certain kinds of class files can be run directly from a Web browser.

8 3.1 Java, the Programming System
There are a number of compilers available that will convert high-level Java code into JVM byecode. There are also programs that will convert other languages into JVM bytecode.

9 3.2.1 The Assembler Conversion process between assembly language and machine code is fairly straightforward. Sun has not established an official, standard assembler for the JVM so we will use jasmin. Written in 1996 by Jon Meyer and Troy Downing, of the NYU Media Research Laboratory.

10 3.2.2 Running a program

11 3.2.2 Running a program

12 3.2.2 Running a program Programs written in jasmin are usually saved with a .j extension. The same steps must be followed as in executing a Java program.

13 3.2.2 Running a program Produce a file, named jasminExample.class, which contains JVM executable code. Can be run by any of the usual means.

14 3.2.3 Display to the console vs. a Window
Most modern programmers prefer to use windows interfaces and interact with their computer via mouse. Java has extensive support for windowed and networked applications.

15 3.2.3 Display to the console vs. a Window
The most popular kind of Web application for Java is called an applet. A small, rather lightweight application, specifically designed for interoperation with web browsers. The method of running the applet is different Instead of the “main” method, the browser invokes “paint” method as the starting point for the code. The output instructions (such as println) are replaced by graphics-specific instructions such as drawstring.

16 3.2.3 Display to the console vs. a Window
Once the class file jasminAppletExample.class has been created, the applet can be run by simply opening the example Web page. Appletviewer a special program supplied with the Java system by Sun.

17 3.2.3 Display to the console vs. a Window

18 3.2.3 Display to the console vs. a Window

19 3.2.3 Display to the console vs. a Window

20 3.2.3 Display to the console vs. a Window

21 3.2.4 Using System.out and System.in
Getting the computer to read and write data can be challenging. Every device may have to be approached on its own individual basis. Java defines a general PrintStream class. Includes general output methods such as print and println. System.out Attached to a default print-capable device. Pushed on to the stack from its static and unchanging location in the system.

22 3.2.4 Using System.out and System.in
The data to be printed must be loaded onto the stack: println method must be invoked: If we had pushed a float: A character string:

23 3.2.4 Using System.out and System.in
With Java 1.5 new methods of doing console input and output use Scanner and Formatter, and printf for formatted output.

24 3.3.1 Instructions and Comments
Assembly language statements can be divided into three types. Instructions are the instructions in the computer's machine language or bytecode. Comments. Any part of the line that follows a semicolon. The computer will never require you to comment programs. By focusing on the large-scale roles of statements and the meanings of blocks and groups of statements, comments are rendered (more) useful and informative.

25 3.3.2 Assembler Directives Third is directive
An instruction to the assembler itself, telling it how to perform its task. Most directives begin with a period. (.class) informs the assembler that this file defines a particular class, the name of the class file to be created. Example: jasminExample.class This doesn't correspond to any bytecode but does directly inform jasmin how to interact with the rest of the computer systems.

26 3.3.2 Assembler Directives The JVM and the class files themselves tie directly into the object-oriented structure and class hierarchy. The Java language makes any class that does not explicitly mention its relationship in the hierarchy, by default, to be a subtype of Object (java/lang/Object). Any defined class must contain a .super directive Other directives are used to define the interactions between this class the the rest of the universe.

27 3.3.3 Resource Directives .limit directive
Limits of resources for computation within a single method. Methods can use a much, or as little resources as they need. A typical stack-based microprocessor or controller would hold only a few elements. Possibly 8.

28 3.3.3 Resource Directives Need more than 8?
Store some in the CPU stack and some elsewhere, such as in main memory. Data would need to be moved in and out of memory as necessary usually being a buggy or entirely dysfunctional program. Increasing the amount of stack space available inside the CPU could solve this problem. Changing fundamental chip parameters between versions will introduce incompatibilities, where newer programs cannot run on legacy hardware.

29 3.3.3 Resource Directives The maximum size of the stack to 14 int- or float-sized elements. The maximum number of (int-sized) local variables. Default limit of one item.

30 3.4.1 Generating Pseudorandom Numbers
Computers are not actually capable of generating “random” data. Pseudorandom data that, although technically speaking can be predicted, appears to be naively unpredictable. The computer takes a given number (the seed) and returns a related but apparently unpredicable number. Uses a so-called linear congruential generator.

31 3.4.1 Generating Pseudorandom Numbers
M determines the maximum size. Oldvalue must be selected anew every time the generator is run. Typical initial seeds current time process ID number most recent movements of the mouse.

32 3.4.2 Implementation on the JVM
Algorithm on the JVM Oldvalue will probably be stored in a local variable. a,c, and m can be stored and manipulated as constants. Oldvalue and newvalue will be stored as ints, but the intermediate values may overflow a single stack element stored as the long type. m = largest prime value = a = = 65537 c = 5

33 3.4.2 Implementation on the JVM

34 3.4.2 Implementation on the JVM
Maximum depth of the stack needed is two long-sized (double) stack elements of 4 or greater. Oldvalue is stored in local variable #1. Local variables are numbered starting at zero In some cases, local variable #0 is reserved by the Java class environment.

35 3.4.3 Another Implementation
If m = 232 No need to take the modulus. Numbers can't be larger that 232. There is no need to use long-sized storage or stack elements if all computations are going to be implicitly done in this modulus. a = and c =0

36 3.4.3 Another Implementation
Which is better? The second generator will always generate an odd number if oldvalue is odd and even if not. From a speed perspective it should be apparent that the second will run faster.

37 3.4.4 Interfacing with Java Class
Generalizing the algorithm Whenever an object's method is invoked, JVM passes the object itself (as a reference type variable) in local variable 0, the method parameters are passes in local variables 1, 2, 3, and so forth. The second generator would need to be placed in a method with at least two stack elements, and at least two local variables, (one for the object, one for the seed value).

38 3.4.4 Interfacing with Java Class

39 3.4.4 Interfacing with Java Class

40 3.4.4 Interfacing with Java Class

41 Chapter Review The JVM was designed hand-in-hand with the language Java. Java programs and jasmin programs must both be converted to .class files before they can be executed by the JVM. The command to convert jasmin programs to class files is usually named jasmin.

42 Chapter Review Input and output is typically a hard problem because of the number of different devices out there. Java and the JVM simplify this problem through the use of the class system. Assembly language instructions can be divided into three major types: Instructions Comments Directives

43 Chapter Review Directives are used to define how class files fit into the standard Java class hierarchy. .limit directives are also used to control the amount of resources available to a given method or function.


Download ppt "Assembly Language Programming in jasmin"

Similar presentations


Ads by Google