ASM1
ASM1
ASM1
My first Assembler program
• AN INTRODUCTION TO ASSEMBLER PROGRAMMING
• 1 AN INTRODUCTION TO ASSEMBLER PROGRAMMING
• 2 THE HEAD …
• 3 .. THE BODY …
• 4 … AND THE TAIL
• 5 MAKE THE NUMBERS
• 6 WRAP IT UP AND CLAIM THE POINTS
IBM Z Xplore
AN INTRODUCTION TO ASSEMBLER PROGRAMMING
A simple example to show that building and running Assembler programs is very similar to the way COBOL,
PL/1 and GO programs are produced.
The Challenge
This challenge will give you an introduction to Assembler programming. It will explain the basics of
how Assembler Code is compiled and executed.
Investment
Steps Duration
ASM1|230329-1718
5 20 minutes
ASM1|230329-1718
SOME BACKGROUND
A relatively simple assembler program will be used and explained.
Each computer architecture has machine instructions unique to the architecture. All computer languages
supported by the architecture must be translated into the unique machine instructions of the underlying
computer architecture.
Each computer architecture has an assembly language which includes “mnemonics” that are assembled into
machine instructions understood by the computer.
Compilers and interpreters translate supported computer languages into the unique machine instructions
understood by the hosting computer.
Higher level languages such as C/C++, Java, COBOL, etc. were created to make programming the computer
easier by hiding the complexity of the underlying machine instructions, addressable memory, and
registers.
Registers are at the top of the memory hierarchy, and provide the fastest way to access data.
ASM1|230329-1718
IBM Z Xplore Copyright IBM 2021-2023 [4/12]
2 THE HEAD …
Below you will find the example code which you will use during this challenge. The code is built to
show the first 40 numbers of the Fibonnaci sequence using an easy calculating model.
ASM1|230329-1718
The first part of the code example demonstrates basic initialisation to have the program being
identified correctly. It determines the start point for entry in register addresses and the storage
pool to use. This section establishes “standard linkage” - a long-standing convention for how one
program (the Shell command line, for example) can hand control over to another program, and for that
program to be able to return control when it finishes back to the right instruction in the caller.
ASM1|230329-1718
This part of the code is the actual application logic.
The first (purple) columns are the instructions which will be executed.
The zSystems CPU architecture uses a wide variety of instructions which can be found in the document
called “Principles of Operations”
In this code you can see some basic instructions which will “move”, “load” or “store” values in
register locations.
• MVI shows that a character value of ” ” (blank) is stored in the predefined register start
location address (RESULT).
• the next statement MVC moves a numeric value of “+1” to a new location
• the next statement prepares an area of memory that will be used to call a program or service
function
This part is being looped until it has executed 38 times - you can see that register 5 (R5) was earlier
initialised with the value 38.
The BCT instruction subtracts 1 from the current R5 value, and if the result is not 0, the program
branches to the labeled location (LOOP)
The first 2 values (0 and 1) for the Fibonacci sequence calculations were given as base starting points
in R2 and R3.
About half of the instructions in the loop are involved with formatting the numbers into displayable
characters, and placing these numbers into the RESULT; these rest perform the actual calculations.
The BAS instruction is what causes the program to call the subroutine that prints the current RESULT
value to stdout.
ASM1|230329-1718
Before the loop ends, the current numeric values for the last number added, and the current total, are
moved into the base numbers for the next iteration.
ASM1|230329-1718
In the last part of the code you will find a subroutine to produce the output using a service program
and a defined storage area for keep several register values, and any other working variables needed by
the program.
This service program BPX1WRT is being used to display the output to the standard output file (the “1”
indicates the file is “stdout”) since the Assembler language does not have a direct instruction for
displaying values like most other programming languages.
• “fibonacci.s”
Use VSCode to copy this to a subdirectory of your home directory called “assembly”.
The first thing you need to do is compile the source code fibonacci.s to a binary file.
Use the terminal function from VSCode to make a SSH connection to the IBM Z Xplore system.
Use the ls command to assure yourself that you are in the correct folder and the source file is
ASM1|230329-1718
displayed.
Enter the following command to compile the source (for this type of code source, compilation is also
known as “assembling”)
as -o fibonacci.o fibonacci.s
as is the command to “assemble source”, where fibonacci.s is the source file and fibonacci.o the binary
output file (the “object” file).
The next step is to create an executable file where the object file is linked with required libraries
(and any other required object modules).
ld -o fibonacci fibonacci.o
Assuming no errors from the linkage process, execute the program by simply typing ./fibonacci
The first 40 numbers of the Fibonacci sequence should be displayed on your terminal screen.
00000000
00000001
00000002
00000003
00000005
[ ... ]
09227465
14930352
24157817
ASM1|230329-1718
39088169
63245986
Let’s get you some credit for that; the usual drill applies:
ASM1|230329-1718
IBM Z Xplore Copyright IBM 2021-2023 [11/12]
Nice job - let’s recap Next up …
ASM1|230329-1718
IBM Z Xplore Copyright IBM 2021-2023 [12/12]