100% found this document useful (6 votes)
11 views

Download (Ebook) An Introduction to Assembly Programming with RISC-V by Edson Borin ISBN 9786500158113, 6500158113 ebook All Chapters PDF

The document provides information about various ebooks related to assembly programming and RISC-V, including titles, authors, and ISBNs, available for download at ebooknice.com. It highlights the importance of learning assembly language for creating high-performance programs and controlling peripheral devices. Additionally, it outlines the contents of 'An Introduction to Assembly Programming with RISC-V' by Edson Borin, which covers computer systems, data representation, assembly language, and user-level programming concepts.

Uploaded by

pauchiakhund
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (6 votes)
11 views

Download (Ebook) An Introduction to Assembly Programming with RISC-V by Edson Borin ISBN 9786500158113, 6500158113 ebook All Chapters PDF

The document provides information about various ebooks related to assembly programming and RISC-V, including titles, authors, and ISBNs, available for download at ebooknice.com. It highlights the importance of learning assembly language for creating high-performance programs and controlling peripheral devices. Additionally, it outlines the contents of 'An Introduction to Assembly Programming with RISC-V' by Edson Borin, which covers computer systems, data representation, assembly language, and user-level programming concepts.

Uploaded by

pauchiakhund
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

Download the Full Ebook and Access More Features - ebooknice.

com

(Ebook) An Introduction to Assembly Programming


with RISC-V by Edson Borin ISBN 9786500158113,
6500158113

https://ebooknice.com/product/an-introduction-to-assembly-
programming-with-risc-v-47667784

OR CLICK HERE

DOWLOAD EBOOK

Download more ebook instantly today at https://ebooknice.com


Instant digital products (PDF, ePub, MOBI) ready for you
Download now and discover formats that fit your needs...

Start reading on any device today!

(Ebook) RISC-V Assembly Language Programming: Unlock the


Power of the RISC-V Instruction Set (Maker Innovations
Series) by Stephen Smith ISBN 9798868801365, 8868801361
https://ebooknice.com/product/risc-v-assembly-language-programming-
unlock-the-power-of-the-risc-v-instruction-set-maker-innovations-
series-55486178
ebooknice.com

(Ebook) RISC-V Assembly Language Programming: Unlock the


Power of the RISC-V Instruction Set (Maker Innovations
Series) by Smith, Stephen ISBN 9798868801365,
9788868801373, 886880137X, 8868801361
https://ebooknice.com/product/risc-v-assembly-language-programming-
unlock-the-power-of-the-risc-v-instruction-set-maker-innovations-
series-55957752
ebooknice.com

(Ebook) An Introduction to Programming with C++ by Diane


Zak ISBN 9781285860114, 128586011X

https://ebooknice.com/product/an-introduction-to-programming-
with-c-5291684

ebooknice.com

(Ebook) Introduction to 64 Bit Windows Assembly


Programming, Fourth Edition by Ray Seyfarth ISBN
9781543138849, 1543138845
https://ebooknice.com/product/introduction-to-64-bit-windows-assembly-
programming-fourth-edition-7142452

ebooknice.com
(Ebook) An Introduction to Network Programming with Java
by Jan Graba ISBN 9780321116147, 0321116143

https://ebooknice.com/product/an-introduction-to-network-programming-
with-java-1267336

ebooknice.com

(Ebook) Guide to Computer Processor Architecture: A RISC-V


Approach, with High-Level Synthesis by Bernard Goossens
ISBN 9783031180224, 3031180224
https://ebooknice.com/product/guide-to-computer-processor-
architecture-a-risc-v-approach-with-high-level-synthesis-49033604

ebooknice.com

(Ebook) Introduction to Computation and Programming Using


Python With Application to Understanding Data by John V.
Guttag ISBN 9780262529624, 0262529629
https://ebooknice.com/product/introduction-to-computation-and-
programming-using-python-with-application-to-understanding-
data-5852392
ebooknice.com

(Ebook) An Introduction to Programming with C++, 6th


Edition by Diane Zak ISBN 9780538466523, 0538466529

https://ebooknice.com/product/an-introduction-to-programming-
with-c-6th-edition-2427428

ebooknice.com

(Ebook) The RISC-V Reader: An Open Architecture Atlas by


David Patterson; Andrew Waterman ISBN 9780999249109,
099924910X
https://ebooknice.com/product/the-risc-v-reader-an-open-architecture-
atlas-32858562

ebooknice.com
book-cover.s •

Users > edson > Desktop > book-cover.s


1 .file "book-cover.c"
2 .option nopic
3 .attribute arch, "rv32i2p0"
4 .attribute unaligned_access, 0
5 .attribute stack_align, 16
6 .text
7 .align 2
- compute_the_answer_to_the_ultimate_question_of_life_the_universe_and_everything:
9 li a0, 42
10 ret
■1 4 .align 2
12 .globl do_something_1000_times
13 • type do_something_1000_times, @function
do_something_l000_times:
add! sp,sp,-16
16 sw s0,8(sp)
1 -t
18
1Q ■ L6:
sw
li
ra,12(sp)
50,1000 An Introduction to
20
21
addi
call
S0,S0,-1
do_something Assembly Programming
bne s0,zero,.L6
23
24
Iw
Iw
ra,12(sp)
s0,8(sp)
with RISC-V
25 addi sp,sp,16
26 jr ra
.section .rodata.strl.4,"aMS”,@progbits,1
28 .align 2
29 . LC0:
30 .ascii "There are 10 types of people in this world "
oq .asciz "those who understand binary and those who don't"
3? .align 2
.LC1:
.string "Assembly language you must learn!"
.align 2
36 .LC2:
37 .ascii "The Unicamp CS course was created in 1969 - "
38 .asciz "The first one in Brazil!"
39 .LC3:
40 .byte 78, 105, 99, 101, 33, 32, 89, 111, 117, 32, 107
41 .byte 110, 111, 119, 32, 65, 83, 67, 73, 73, 33, 0
42 .align 2
43 •sect! •text.startup,"ax". @pгодbits
44 .align 2
.globl main
46 • type main, ^function
47 main:
48 lui a0,%hi(.LC0)
49 addi sp,sp,-16
50 addi a0,a0,%lo(.LC0) Prof. Edson Borin
51 sw ra,12(sp)
call printf Institute of Computing
53 lui a0,%hi(.LCl)

55
addi
call
a0,a0,%lo( .LCD
printf
Unicamp
lui a0,%hi(.LC2)
57 addi a0,a0,%lo(.LC2)
58 call printf
59 lui a0,Mii(.LC3)
60 addi a0,a0,%lo(.LC3)
61 call printf
62 Iw ra,12(sp)
63 li a0,0

65
addi sp,sp,16
ra
1st edition
jr
An Introduction to Assembly Programming
with RISC-V

2
Copyright © 2021 Edson Borin

All rights reserved. This book or any portion thereof may not be reproduced
or used in any manner whatsoever without the express written permission of
the author except for the use of brief quotation in a book review.

ISBN:978-65-00-15811-3
First edition 2021

Edson Borin
Institute of Computing - University of Campinas
Av. Albert Einstein, 1251
Cidade Universitaria Zeferino Vaz
Barao Geraldo - Campinas - SP - Brasil
www.ic.unicamp.br/~edson
13083-852

An updated version of this book and other material may be available at:
www.riscv-programming.org

3
Foreword

This book focuses on teaching the art of programming in assembly language, using
the RISC-V ISA as the guiding example. Towards this goal, the text spans, at an
introductory level, the organization of computing systems, describes the mechanics of
how programs are created and introduces basic programming concepts including both
user level and system programming. The ability to read and write code in low-level
assembly language is a powerful skill to be able to create high performance programs,
and to access features of the machine that are not easily accessible from high-level
languages such as C, Java or Python, for example to control peripheral devices.
The book introduces the organization of computing systems, and the mechan­
ics of creating programs and converting them to machine-readable format suitable
for execution. It also teaches the components of a program, or how a programmer
communicates her intent to the system via directives, data allocation primitives and
finally the ISA instructions, and their use. Basic programming concepts of control
flow, loops as well as the runtime stack are introduced.
Next the book describes the organization of code sequences into routines and
subroutines, to compose a program. The text also addresses issues related to system
programming, including notions of peripheral control and interrupts.
This text, and ancillary teaching materials, has been used in introductory classes
at the University of Campinas, Brazil (UNICAMP) and has undergone refinement
and improvement for several editions.

Mauricio Breternitz
Principal Investigator & Invited Associate Professor
ISTAR ISCTE Laboratory
ISCTE Institute Universitario de Lisboa
Lisbon, Portugal

4
Notices:

• Document version: May 9, 2022

• Please, report typos and other issues to Prof. Edson Borin ([email protected].
br).

5
Contents

Foreword 4

Glossary 11

Acronyms 14

I Introduction to computer systems and assembly language


1

1 Execution of programs: a 10,000 ft overview 2


1.1 Main components of computers....................................................................... 2
1.1.1 The main memory.................................................................................. 3
1.1.2 The CPU................................................................................................... 3
1.2 Executing program instructions....................................................................... 4
1.3 The boot process................................................................................................... 5

2 Data representation on modern computers 6


2.1 Numeral Systems and the Positional Notation............................................ 6
2.1.1 Converting numbers between bases.................................................... 8
2.2 Representing numbers on computers............................................................. 11
2.2.1 Unsigned numbers................................................................................... 11
2.2.2 Signed numbers......................................................................................... 12
2.2.3 Binary arithmetic and Overflow........................................................... 14
2.2.4 Integer Overflow...................................................................................... 15
2.3 Representing text................................................................................................... 16
2.4 Organizing data on the memory........................................................................ 18
2.4.1 Texts on the main memory................................................................. 18
2.4.2 Numbers on the main memory........................................................... 19
2.4.3 Arrays on the main memory................................................................. 19
2.4.4 Structs on the main memory.............................................................. 21
2.5 Encoding instructions ........................................................................................ 22

3 Assembly, object, and executable files 24


3.1 Generating native programs.............................................................................. 24
3.1.1 Inspecting the contents of object and executable files................. 26
3.2 Labels, symbols, references, and relocation................................................... 27
3.2.1 Labels and symbols .............................................................................. 27
3.2.2 References to labels and relocation................................................... 28
3.2.3 Undefined references.............................................................................. 30
3.2.4 Global vs local symbols........................................................................ 31
3.2.5 The program entry point .................................................................... 32
3.3 Program sections................................................................................................... 33
3.4 Executable vs object files ................................................................................. 36

6
4 Assembly language 37
4.1 Comments................................................................................................................. 39
4.2 Assembly instructions......................................................................................... 39
4.3 Immediate values................................................................................................... 40
4.4 Symbol names ....................................................................................................... 40
4.5 Labels........................................................................................................................ 41
4.6 The location counter and theassembling process........................................ 42
4.7 Assembly directives................................................................................................ 44
4.7.1 Adding values to the program.............................................................. 44
4.7.2 The .section directive........................................................................ 46
4.7.3 Allocating variables on the.bss section........................................... 47
4.7.4 The .set and .equ directives.............................................................. 48
4.7.5 The .globl directive............................................................................... 49
4.7.6 The .align directive............................................................................... 49

II User-level programming 51
5 Introduction 52

6 The RV32I ISA 53


6.1 Datatypes and memory organization................................................................. 54
6.2 RV32I registers....................................................................................................... 55
6.3 Load/Store architecture..................................................................................... 55
6.4 Pseudo-instructions................................................................................................ 56
6.5 Logic, shift, and arithmetic instructions....................................................... 56
6.5.1 Instructions syntax and operands....................................................... 57
6.5.2 Dealing with large immediate values................................................. 57
6.5.3 Logic instructions ................................................................................... 58
6.5.4 Shift instructions....................................................................................... 59
6.5.5 Arithmetic instructions ......................................................................... 61
6.6 Data movement instructions............................................................................... 63
6.6.1 Load instructions..................................................................................... 63
6.6.2 Store instructions...................................................................................... 67
6.6.3 Data movement pseudo-instructions................................................ 68
6.7 Control-flow instructions..................................................................................... 69
6.7.1 Conditional control-flow instructions................................................ 69
6.7.2 Direct vs indirect control-flowinstructions ..................................... 71
6.7.3 Unconditional control-flow instructions............................................ 72
6.7.4 System Calls............................................................................................ 73
6.8 Conditional set instructions............................................................................... 74
6.9 Detecting overflow................................................................................................ 74
6.10 Arithmetic on multi-word variables................................................................. 75

7 Controlling the execution flow 77


7.1 Conditional statements......................................................................................... 77
7.1.1 if-then statements................................................................................ 77
7.1.2 Comparing signed vs unsignedvariables............................................ 77
7.1.3 if-then-else statements...................................................................... 78
7.1.4 Handling non-trivial booleanexpressions............................................ 79
7.1.5 Nested if statements................................................................................ 80
7.2 Repetition statements......................................................................................... 81
7.2.1 while loop ................................................................................................. 81
7.2.2 do-while loop.......................................................................................... 81
7.2.3 for loop........................................................................................................ 82
7.2.4 Hoisting loop-invariant code.................................................................. 83
7.3 Invoking and returning from routines.............................................................. 83
7.3.1 Returning values from functions ........................................................ 84
7.4 Examples................................................................................................................. 85

7
7.4.1 Searching for the maximum value on an array............................... 85

8 Implementing routines 87
8.1 The program memory layout ........................................................................... 87
8.2 The program stack................................................................................................ 87
8.2.1 Types of stacks......................................................................................... 90
8.3 The ABI and software composition................................................................. 91
8.4 Passing parameters to and returning values from routines.................... 91
8.4.1 Passing parameters to routines........................................................... 91
8.4.2 Returning values from routines........................................................... 93
8.5 Value and reference parameters........................................................................ 93
8.6 Global vs local variables..................................................................................... 95
8.6.1 Allocating local variables on memory................................................. 96
8.7 Register usage policies......................................................................................... 98
8.7.1 Caller-saved vs Callee-saved registers................................................. 99
8.7.2 Saving and restoring the return address.............................................. 100
8.8 Stack Frames and the Frame Pointer................................................................. 100
8.8.1 Stack Frames................................................................................................. 100
8.8.2 The Frame Pointer....................................................................................... 101
8.8.3 Keeping the stack pointer aligned........................................................... 102
8.9 Implementing RISC-V ilp32 compatible routines..........................................102
8.10 Examples................................................................................................................... 103
8.10.1 Recursive routines...................................................................................... 103
8.10.2 The standard “C” library syscall routines..........................................104

III System-level programming 106


9 Accessing peripherals 107
9.1 Peripherals ................................................................................................................. 107
9.2 Interacting with peripherals.................................................................................. 108
9.2.1 Port-mapped I/O......................................................................................... 109
9.2.2 Memory-mapped I/O ............................................................................... 110
9.3 I/O operations on RISC-V...................................................................................... 111
9.4 Busy waiting..............................................................................................................112

10 External Interrupts 114


10.1 Introduction................................................................................................................114
10.1.1 Polling..............................................................................................................116
10.2 External Interrupts.................................................................................................. 116
10.2.1 Detecting external interrupts................................................................. 117
10.2.2 Invoking the proper interruptservice routine.................................... 118
10.3 Interrupts on RV32I............................................................................................... 120
10.3.1 Control and Status Registers................................................................. 120
10.3.2 Interrupt related Control andStatus Registers ................................. 121
10.3.3 Interrupt Handling Flow............................................................................122
10.3.4 Implementing an interrupt service routine......................................... 123
10.3.5 Setting up the Interrupt Handling Mechanism................................... 124

11 Software Interrupts and Exceptions 127


11.1 Privilege Levels......................................................................................................... 127
11.2 Protecting the system........................................................................................... 128
11.3 Exceptions ................................................................................................................ 128
11.4 Software Interrupts.................................................................................................. 129
11.5 Protecting RISC-V systems................................................................................. 130
11.5.1 Changing the privilege mode................................................................. 130
11.5.2 Configuring the exception and software interrupt mechanisms . 131
11.5.3 Handling illegal operations ..................................................................... 131
11.5.4 Handling system calls............................................................................... 132

8
A RV32IM ISA reference card 134

9
10
Glossary

32-bit address space is set of addresses represented by 32-bit unsigned numbers.


10, 54

binary digit is a digit that may assume one of two values: “0” (zero) or “1” (one).
10, 11, 14

bus is a communication system that transfers information between the computer


components. This system is usually composed of wires that are responsible for
transmitting the information and associated circuitry, which are responsible for
orchestrating the communication. 3, 10, 107-109, 112

byte addressable memory is a memory in which each memory word stores a single
byte. 3-5, 10, 18-21, 23, 54

Central Processing Unit , or CPU, is the computer component responsible for


executing the computer programs. 2, 3, 10, 13

column-major order specifies that the elements of a two-dimensional array are


organized in memory column by column. In this context, the elements of the
first column are placed first then the elements of the second column are placed
after the elements of the first one and so on. 10

Control and Status Register , or CSR, is an internal CPU register that exposes
the CPU status to the software and allow software to control the CPU. 10, 120,
121, 129, 131

endianness refers to the order in which the bytes are stored on a computing system.
There are two common formats: little-endian and big-endian. The little-endian
format places the least significant byte on the memory position associated with
the lowest address while the big-endian format places the most significant byte
on the memory position associated with the highest address. 10, 19, 46, 64-67

exceptions are events generated by the CPU in response to exceptional conditions


when executing instructions. 10

external interrupts are interrupts caused by external (non-CPU) hardware, such


as peripherals, to inform the CPU they require attention. 10

hardware interrupts are events generated by hardware, such as peripherals, to


inform the CPU they require attention. 10

immediate value is a number that is encoded into the instruction encoding. As a


consequence, it is a constant. 10, 57-69, 72, 135

Instruction Set Architecture defines the computer instructions set, including, but
not limited to, the behavior of the instructions, their encoding, and resources
that may be accessed by the instructions, such as CPU registers. 4, 10, 49, 53,
54, 56, 60, 62, 64-67, 70-72, 74, 75, 84, 120, 127, 128

11
integer overflow occurs when the result of an arithmetic operation on two integer
m-bit binary numbers is outside of the range that can be represented by an
m-bit binary number. 10, 15, 16

interrupt service routine , or ISR, is a software routine that handles interrupts.


It is also nown as interrupt handler. 10, 117, 118, 121-125, 128, 129, 131, 132

interrupt vector table is a table that maps interrupt/exception identifiers to rou­


tines that must be invoked to handle the interrupt/exception. The interrupt
vector table is usually stored in main memory and accessed by the CPU hard­
ware to invoke the proper routine when handling an interrupt/exception. 10,
131

ISA native datatype is a datatype that can be naturally processed by the ISA. 10,
54, 64

load instruction is an instruction that loads a value from main memory into a
register. 10, 56

Load/Store architecture is a computer architecture that requires values to be


loaded/stored explicitly from/to main memory before operating on them. 10,
55, 56

machine language is a low-level language that can be directly processed by the


computer’s central processing unit (CPU). 10, 25, 26

main memory is a storage device used to store the instructions and data of pro­
grams that are being executed. 2-5, 10, 12, 18, 20, 21, 23, 27, 32, 33, 35, 36,
48, 107-112, 114-119, 123-125, 128

native program is a program encoded using instructions that can be directly exe­
cuted by the CPU, without help from an emulator or a virtual machine. 2, 10,
24, 26

numeral system is a system used for expressing numbers. 6-12

opcode the opcode, or operation code, is a code (usually encoded as a binary num­
ber) that indicates the operation that an instruction must perform. 10, 57

peripherals are input/output, or I/O, devices that are connected to the computer.
Examples of peripheral devices include video cards (also known as graphics
cards), USB controllers, network cards, etc.. 2, 10, 107

persistent storage is a storage device capable of preserving its contents when the
power is shut down. Hard disk drives (HDDs), solid state drives (SSDs), and
flash drives are example of persistent storage devices. 2, 3, 10, 107

positional numeral system is a numeral system in which the value of a digit di


depends on the its position on the sequence. 7, 8, 10, 11

privilege level defines which ISA resources are accessible by the software being ex­
ecuted. 10, 120, 127, 128

privilege mode defines the privilege level for the currently executing software. 10,
13, 128-131

program counter or PC, is the register that holds the address of the next instruc­
tion to be executed. In other words, it holds the address of the memory position
that contains the next instruction to be executed. It is also known as instruction
pointer, or IP, in some computer architectures. 10, 55

12
pseudo-instruction is an assembly instruction that does not have a corresponding
machine instruction on the ISA, but can be translated automatically by the
assembler into one, or more, alternative machine instructions to achieve the
same effect. 10, 39, 40, 56, 58, 68, 69

register is a small memory device usually located inside the Central Processing Unit
(CPU) for quick read and write access. 3, 10

row-major order specifies that the elements of a two-dimensional array are orga­
nized in memory row by row. In this context, the elements of the first row are
placed first then the elements of the second row are placed after the elements
of the first one and so on. 10, 21

stack pointer is a pointer that points to the top of the program stack. In other
words, it holds the address of the top of the program stack. In RISC-V, the
stack pointer is stored by the sp register.. 10

store instruction is an instruction that stores values into main memory. 10

unprivileged ISA is the sub-set of the ISA that is accessible by the software running
on unprivileged mode. 10, 55, 128

unprivileged mode is the privilege mode with least privileges. In RISC-V, it is the
User/Application privilege mode. 10, 13, 128

unprivileged registers are a set of registers accessible on the unprivileged mode.


10, 55

user application is an application designed to be executed at user-mode on a system


managed by an operating system. 10

user-mode on RISC-V, the user-mode is equivalent to the User/Application mode.


10, 13, 128

13
Acronyms

ABI Application Binary Interface. 10, 54, 84, 91-94, 99, 102, 103, 125

ASCII American Standard Code for Information Interchange. 10, 16-18

bit Binary digit. 2-5, 10-20, 22, 23, 25, 28, 29, 35, 37, 40, 44-46, 48-50, 102, 108-114,
120, 121, 125

CPU Central Processing Unit. 2-5, 10-13, 32, 36, 49, 52, 107-112, 114-125, 128-132

CSR Control and Status Register. 10, 120-125, 129-132

ISA Instruction Set Architecture. 4, 10, 13, 24-26, 38-40, 49, 50, 55, 109-111, 120,
124, 125, 128, 129

ISR Interrupt Service Routine. 10, 117-120, 124, 125

PC Program Counter. 4, 5, 10, 27, 32, 117, 121-124, 129, 131

UTF-8 Universal Coded Character Set (or Unicode) Transformation Format - 8-bit.
10, 16-18

14
Part I

Introduction to computer
systems and assembly
language

1
Chapter 1

Execution of programs: a
10,000 ft overview

There are several ways of encoding a computer program. Some programs, for ex­
ample, are encoded using abstract instruction sets and are executed by emulators or
virtual machines, which are other programs designed to interpret and execute the
abstract instruction set. Bash scripts, Java byte-code programs, and Python scripts
are common examples of programs that are encoded using abstract instruction sets
and require an emulator or a virtual machine to support their execution.
A native program is a program encoded using instructions that can
be directly executed by the computer hardware, without help from an
emulator or a virtual machine. In this book, we focus our discussion on native
programs. Hence, from now on, whenever we use the term “program”, unless stated
otherwise, we are referring to native programs.
Native program instructions usually perform simple operations, such as adding or
comparing two numbers, nonetheless, by executing multiple instructions, a computer
is capable of solving complex problems.
Most modern computers are built using digital electronic circuitry. These machines
usually represent information using voltage levels that are mapped to two states,
HIGH and LOW, or “1” (one) and “0” (zero). Hence, the basic unit of information
on modern computers is a binary digit, i.e., “1” or “0”. Consequently, information
and instructions are encoded as sequences of binary digits, or bits.

1.1 Main components of computers


Computers are usually composed of the following main components:

• Main memory: The main memory is used to store the instructions and data of
programs that are being executed. The main memory is usually volatile, hence,
if the computer is turned off, its contents are lost.

• Central Processing Unit: the Central Processing Unit, or CPU, is the com­
ponent responsible for executing the computer programs. The CPU retrieves
programs’ instructions from the main memory for execution. Also, when execut­
ing instructions, the CPU often reads/writes data from/to the main memory.

• Persistent storage: Since the main memory is volatile, there is usually a


persistent storage device to preserve the programs and data when the power is
shut down. Hard disk drives (HDDs), solid state drives (SSDs), and flash drives
are example of persistent storage devices.

• Peripherals: Peripherals are input/output, or I/O, devices that are connected


to the computer. Examples of peripheral devices include video cards (also known
as graphics cards), USB controllers, network cards, etc..

2
1.1. MAIN COMPONENTS OF COMPUTERS

• Bus: The bus is a communication system that transfers information between


the computer components. This system is usually composed of wires that are
responsible for transmitting the information and associated circuitries, which
orchestrate the communication.

Figure 1.1 illustrates a computer system in which the CPU, the main memory, a
persistent storage device (HDD) and two I/O devices are connected through a system
bus.

Figure 1.1: Computer system components connected through a system bus.

1.1.1 The main memory


The computer main memory is a storage device used to store the program instructions
and data, and it is composed of a set of memory words. Each memory word is capable
of storing a set of bits (usually eight bits) and is identified by a unique number, known
as the memory word address. A byte addressable memory is a memory in which each
memory word (a.k.a. memory location) stores a single byte and is associated with a
unique address. Figure 1.2 illustrates the organization of a byte addressable memory.
Notice that the memory word identified by address 5 (or simply “memory word 5”)
contains the value 111111112 while memory word 0 contains the value 001101102.

Addresses Memory
locations

(a)
Figure 1.2: Organization of a byte addressable memory with its contents represented
in the binary (a) and the hexadecimal (b) bases.

1.1.2 The CPU


The Central Processing Unit is the component responsible for executing the computer
programs. There are several ways of implementing and organizing a CPU, however,
to understand how programs are executed, it suffices to know that the CPU contains:

• Registers: a CPU register is a small memory device located inside the CPU.
The CPU usually contains a small set of registers. RISC-V processors, for ex­
ample, contain thirty-one 32-bit registers1 that can be used by programs to
store information inside the CPU. Computers often contain instructions that*

1A 32-bit register is a register that is capable of storing 32 bits, i.e., values composed of 32 bits.

http://riscv-programming.org (Version: May 9, 2022) 3


CHAPTER 1. EXECUTION OF PROGRAMS: A 10,000 FT OVERVIEW

copy values from the main memory into CPU registers, known as “load” in­
structions, and instructions that copy values from the CPU registers into the
main memory, known as “store” instructions.

• A datapath: the CPU datapath is responsible for performing operations, such


as arithmetic and logic operations, on data. The datapath usually performs
the operation using data from the CPU registers and store the results on CPU
registers.

• A control unit: the control unit is the unit responsible for orchestrating the
computer operation. It is capable of controlling the datapath and other compo­
nents, such as the main memory, by sending commands through the bus. For
example, it may send a sequence of commands to the datapath and to the main
memory to orchestrate the execution of a program instruction.

Accessing data on registers is much faster than accessing data on the main memory.
Hence, programs tend to copy data from memory and keep them on CPU registers to
enable faster processing. Once the data is no longer needed, it may be discarded or
saved back on the main memory to free CPU registers.
The Instruction Set Architecture, or ISA, defines the computer instructions set,
including, but not limited to, the behavior of the instructions, their encoding, and
resources that may be accessed by the instructions, such as CPU registers. A program
that was generated for a given ISA can be executed by any computer that implements
a compatible ISA.
ISAs tend to evolve over time, however, ISA designers try to keep newer ISA
versions compatible with previous ones so that legacy code, i.e., code generated for
previous versions of the ISA, can still be executed by newer CPUs. For example, a
program that was generated for the 80386 ISA can be executed by any processor that
implements this or any other compatible ISAs, such as the 80486 ISA.

1.2 Executing program instructions


As discussed previously, modern computers usually store the program that is being
executed on main memory, including its instructions and data. The CPU retrieves
programs’ instructions from the main memory for execution. Also, when executing
instructions, the CPU may read/write data from/to the main memory. To illustrate
this process we will consider a CPU that implements the RV32I ISA.
The RV32I ISA specifies that instructions are encoded using 32 bits. Hence,
assuming the system has a byte addressable memory2 , each instruction occupies four
memory words. Also, it specifies that instructions are executed sequentially3, in the
same order they appear in the main memory.
Let us consider a small program generated for the RV32I ISA that is composed of
three instructions and is stored in main memory starting at address 8000. Since each
instruction occupies four bytes (i.e., 32 bits) and instructions are stored consecutively
on main memory, the first instruction is located at addresses 8000, 8001, 8002, and
8003, the second one on addresses 8004, 8005, 8006, and 8007, and the third one on
addresses 8008, 8009, 800A, and 800B. Figure 1.3 illustrates the instructions stored
on the main memory.
The CPU usually contains a register to keep track of the next instruction that
needs to be executed. This register, called Program Counter, or PC, on the RV32
ISA, stores the starting address of the sequence of memory words that store the next
instruction to be executed. For example, before executing the first instruction of the
code illustrated at Figure 1.3, the PC contains the value 8000. Once the instruction
stored at address 8000 is fetched, the value of the PC is updated by adding four to its
contents so that the next instruction (at address 8004) can be fetched for execution
once the current instruction is completed. Algorithm 1 illustrates the execution cycle

2 This is usually the case in modern computers.


3As discussed in Section 6.7, control-flow instructions may change the normal execution flow.

http://riscv-programming.org (Version: May 9, 2022) 4


1.3. THE BOOT PROCESS

Memory
Address
words
■ ■■ ...
000100112 8000
000001012 8001
000101012 8002
00000000., 8003
Assembly language (RV32I)
loop: / 100100112 8004
addi a0, a0, 1 100001012 8005
addi al, al, 1
beq a0, al, loop •. 111101012 8006
11111111 2 8007
111000112 8008
000011002 8009
101101012 8 0 0A
111111102 800B
■ ■■ ...

Figure 1.3: Three RV32I instructions stored on a byte addressable memory starting
at address 8000.

performed by a simple RV32I CPU. First, the CPU uses the address in the PC to fetch
an instruction (a sequence of four memory words, i.e., 32 bits) from main memory
and store it on an internal register called IR. Then, it updates the PC so it points to
the next instruction in memory. Finally, it executes the instruction that was fetched
from memory. Notice that when executing the instruction, the CPU may also access
the main memory to retrieve or store data.
Algorithm 1: RV32I instructions execution cycle.
1 while True do
2 // Fetch instruction and update PC ;
3 IR ^ MainMemory[PC] ;
4 PC ^ PC+4;
5 ExecuteInstruction(lR);
6 end

To execute a program, the operating system essentially loads the program into the
main memory (e.g., from a persistent storage device) and sets the PC so it points to
the program entry point.

1.3 The boot process


Since the main memory is volatile, whenever a computer is powered on, it contains
garbage. As a consequence, at this point, the CPU may not retrieve instructions from
the main memory. In this context, on power on, the PC is automatically set so that
the CPU starts by retrieving instructions from a small non-volatile memory device,
which stores a small program that performs the boot process4. This program sets up
the basic computer components, checks the boot configuration (also stored on a non­
volatile memory), and, based on its settings, loads into main memory the operating
system boot loader from a persistent storage device (e.g., the hard disk drive).
Once the operating system boot loader is loaded into memory, the CPU starts
executing its code, which, in turn, finishes setting up the computer and loading the
primary operating system modules into the main memory. Once the boot process
finishes, a copy of the primary operating system modules is located in the main
memory, and the system is ready to execute other programs, such as users’ programs.
4 In old personal computer systems this program is known as the Basic Input/Output System, or
BIOS. More modern computers use the Unified Extensible Firmware Interface, or UEFI, standard.

http://riscv-programming.org (Version: May 9, 2022) 5


Chapter 2

Data representation on
modern computers

This chapter discusses how information is represented on computers. First, Section 2.1
introduces the concepts of numeral systems and the positional notation. Then, sec­
tions 2.2 and 2.3 discuss how numbers and text are represented on computers, respec­
tively. Next, Section 2.4 shows how data is organized in memory. Finally, Section 2.5
discusses how instructions are encoded.

2.1 Numeral Systems and the Positional Notation


A numeral system is a system used for expressing numbers. The numeral
system defines a set of symbols and rules for using them to represent a given set of
numbers (e.g., the natural numbers). For example, the “unary numeral system”, is
a numeral system in which every natural number is represented by a corresponding
number of symbols. In this system, assuming the base symbol is the ? character,
natural numbers one, two, and five would be represented as ?, ??, and ?????,
respectively.
The “decimal numeral system” is the most common numeral system used by hu­
mans to represent integer and non-integer numbers. Let:

• D1o be the set of symbols used in the decimal numeral system, i.e., D1o =
{“0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”}; and

• di be a digit on a number represented in the decimal numeral system, i.e.,


di e D10;

• numberio be a number represented in the decimal numeral system;

any natural number may be represented on the decimal numeral system by an


ordered sequence of m digits, as illustrated by Equation 2.1. The superscript anno­
tation indicates the digit position. Notice that the rightmost digit is associated with
position 0 and the leftmost digit is associated with position m — 1.

numberio = dm-1dm-2 • • • d1d°Q (2.1)

For example, the number one thousand nine hundred and sixty nine is represented
by the sequence 1969. In this case, m = 4, d3 = “1”, d2 = “9”, d1 = “6”, and
d0 = “9”
Let:

• symbol-value(d') be the value of the symbol used in digit di; and

• symbol value(“0”) = zero, symbol value(“1”) = one, symbol value(“2”) =two,


..., symbol _value(“9”) = nine;

6
2.1. NUMERAL SYSTEMS AND THE POSITIONAL NOTATION

The value of a number with m digits in the decimal numeral system is computed
by Equation 2.2.
i<m

value(number10) — \ symbol _value(di) x 10i (2.2)


i=0| -------
" {z------ }
digit value

Notice that the value of each digit di, i.e., the contribution of each digit di to the
number value, depends on the symbol used and its position (index i) on the sequence.
For example, the number 1969 has four digits: d3 = 1,d2 = 9, d1 = 6, d0 = 9. The
value of digit d2 is nine hundred while the value of digit d0 is nine.
A positional numeral system is a numeral system in which the value of
a digit di depends on the value of the symbol used on the digit and also
on the position of the digit on the sequence of digits. The decimal numeral
system is a positional numeral system. The unary and the Roman numeral systems,
on the other hand, are common examples of non-positional numeral systems.
The base, or radix, of a numeral system is the number of different
symbols a digit may assume to represent the numbers. The base of the
“unary numeral system” is one while the base of the “decimal system” is ten.
The decimal, binary, octal and hexadecimal numeral systems are posi­
tional numeral systems that are frequently used when programming com­
puters. The only difference between these numeral systems is the base. While the
base of the decimal numeral system is ten, the base of the binary, the octal, and the
hexadecimal numeral systems are two, eight, and sixteen, respectively.
Let:
• base be the base, or radix, of the positional numeral system;

• Dbase be the set of symbols used in the positional numeral system (e.g., D2 =
{“0”, “1”}); and

• dbase be the ith digit on a number represented in the positional numeral system,
i.e., dbase G Dbase;

• numberbase be a number represented in the numeral system;

any natural number may be represented on the decimal/binary/octal/hexadeci-


mal positional numeral system by an ordered sequence of m digits, as illustrated by
Equation 2.3.

number =d
numberbase — dm 1 dm 2 • • • d
base dbase
1 d^
dbase °
dbase (2 3))
(2.3
Let:

• symbol-value(dbase) be the value of the symbol used in digit dbase; and

• symbol value(“0”) — zero, symbol value(“1”) — one, and so on;

The value of a natural number with m digits in any of these positional numeral
systems is defined by Equation 2.4.

i<m

value(numberbase) — / symbol -va,lue(dbase) x base (2.4)


I--- 1 I------------------------
------ {z ------------ }
i=0
digit value

For example, the value of any natural number represented on the binary numeral
system (base — 2) is defined by Equation 2.5

i<m

value(number2) — \ symbol value(d2) x 2i (2.5)


I—i i _________ ___________ }
i=0 1- 4 value
digit 1

Notice that the value of the sequence 11 is three on the binary numeral system
(1 x 21 +1 x 20) while it is eleven on the decimal numeral system (1 x 101 + 1 x 10 0)
and seventeen on the hexadecimal numeral system (1 x 161 + 1 x 160).

http://riscv-programming.org (Version: May 9, 2022) 7


CHAPTER 2. DATA REPRESENTATION ON MODERN COMPUTERS

When working with multiple numeral systems it is often necessary to annotate the
numbers so that it is possible to identify the numeral system being used, and hence, its
value. A common notation is to append a subscribed suffix to the number indicating
the base of the positional numeral system. For example, the value of number 1110 is
eleven while the value of number 112 is three1.
Appending a subscribed suffix to the number is not a natural way of annotating
numbers in computer programs. In these cases, a common approach is to append a
prefix that indicates the base. For example, in “C”, the programmer may use the
prefix “0b”/“0”/“0x” to indicate that the number is in the binary/octal/hexadecimal
base, i.e., base 2/8/16. In “C”, numbers that lack a prefix belong to the decimal
numeral system.
The binary and octal numeral systems use a subset of the symbols used on the
decimal numeral system to represent the numbers. The hexadecimal numeral system,
on the other hand, requires more than ten symbols, hence, new symbols are needed.
In this case, the first letters of the alphabet are used to complement the set of symbols.
Table 2.1 shows the symbols used in each one of these positional numeral systems and
their corresponding values.

Used in base
Symbol symbol-value
2 8 10 16
“0” zero X X X X
“1” one X X X X
“2” two X X X
“3” three X X X
“4” four X X X
“5” five X X X
“6” six X X X
“7” seven X X X
“8” eight X X
“9” nine X X
“A” ten X
“B” eleven X
“C” twelve X
“D” thirteen X
“E” fourteen X
“F ” fifteen X

Table 2.1: Set of symbols used in binary, octal, decimal, and hexadecimal numeral
systems and their respective values.

Table 2.2 shows how values zero to twenty are represented in the hexadecimal,
decimal, octal, and binary numeral systems.

2.1.1 Converting numbers between bases


Converting numbers between positional numeral systems is a common task in several
contexts, specially when programming or debugging a computer system. Since hu­
mans usually prefer to think using the decimal numeral system, they usually convert
numbers from other positional numeral systems to the decimal numeral system to
reason about their values. Also, in some situations, it may be necessary to convert
values to the binary, octal, or the hexadecimal numeral systems.
Equation 2.4 shows how to compute the value of numbers represented on any
positional numeral system.
To represent a value V in a positional numeral system, one must find a sequence
of digits dm -1 dm1-2 • • • dL„„d°„
base base
e„ so that Equation 2.4 holds. In other
base base , words,
b
let v(di)
be the value of the symbol used on the ith digit*2 of a number represented in base b,

1Notice that, accordingly to Equation 2.4, value(112) = 1 x 21 + 1 x 20 = three


2We will use the notation v(db) instead of symboLvalue(db) to keep equations short.

http://riscv-programming.org (Version: May 9, 2022) 8


2.1. NUMERAL SYSTEMS AND THE POSITIONAL NOTATION

Numeral system
Value
Hexadecimal Decimal Octal Binary
zero 016 010 08 02
one 116 110 18 12
two 216 210 28 102
three 316 310 38 112
four 416 410 48 100 2
five 516 510 58 101 2
six 616 610 68 110 2
seven 716 710 78 111 2
eight 816 810 108 10002
nine 916 910 118 10012
ten A16 1010 128 10102
eleven B16 1110 138 10112
twelve C16 1210 148 11002
thirteen D16 1310 158 11012
fourteen E16 1410 168 11102
fifteen F16 1510 178 11112
sixteen 1016 1610 208 100002
seventeen 1116 1710 218 100012
eighteen 1216 1810 228 100102
nineteen 1316 1910 238 100112
twenty 1416 2010 248 101002

Table 2.2: Values zero to twenty represented in the hexadecimal, decimal, octal, and
binary numeral systems.

one must find a sequence of digits d^se1 dma-e2 • • • d1ase 0ase so that: d

V = v(dm-1) X bm-1 + ••• + v(d1) x b1 + v(d0) x b0 (2.6)

The previous equation may be rewritten as:

V = b x (v(dm-1) x bm-2 + ••• + v(d) x b0) + v(d0) (2.7)


|-------------------------------- {z------------------------------- }
V0

Notice that v(d0) and V0 are equivalent to the remainder and the quotient of the
division of V by b. Hence, to find out the symbol value of digit d0 (i.e., v(d0)) it
suffices to compute the remainder of the division of V by b.

Using the same reasoning, the symbol value of digit d1 may be computed by
dividing V0 by b. Notice that the remainder of the division of V0 by b is equal to
v(d 1).

Let symbol from, value(v, b) be a function that returns the symbol used to rep­
resent value v on base b (e.g., symbol-from-value(eleven, 16) = “B”), Algorithm 2
shows an algorithm to compute the sequence of digits dml-1dm1-2 • • • d1d0 that repre­

http://riscv-programming.org (Version: May 9, 2022) 9


CHAPTER 2. DATA REPRESENTATION ON MODERN COMPUTERS

sent value V on base b.


Algorithm 2: Algorithm to compute the sequence of digits
dml-1dml-2 • • • d1 d0 that represent value V on base b.
input : Value V and base b.
output: Sequence of digits dml-1dml-2 • • • d1d0.
1 i = 0 ;
2 tmp = V ;
3 while tmp = 0 do
4 rem = tmp mod b ;
5 dbl = sym,bol_from,_value(rem,, b) ;
6 tmp = tmp / b ;
7 i =i+1 ;
8 end

To illustrate the use of Algorithm 2 lets compute the sequence of digits dm1-1 d'm‘ —
2

• • • d1 d0 that represent value twenty six on base 2. First, we divide twenty six by
two. The remainder of this division is zero and the quotient is thirteen, hence, v(d2)
= zero and d0 = “0”. This process is illustrated in Figure 2.1 (a). To compute v(d 1)
we now divide thirteen (i.e., the quotient of the last division) by two. The remainder
of this division is one and the quotient is six, hence, v(d2) = one and d1 = “1”.
This partial result is illustrated in Figure 2.1 (b). To compute v(d2) we now divide
six (the previous quotient) by two. The remainder of this division is zero and the
quotient is three, hence, v(d2) = zero and d2 = “0”. Now, we divide three by two.
The remainder of this division is one and the quotient is one, hence, v(d3) = one
and d3 = “1”. Again, we divide the last quotient (one) by two. The remainder of
this division is one and the quotient is zero, hence, v(d4) = one and d42 = “1”. Since
the last quotient is zero, the process is complete. As a result, the sequence 110102
represents value twenty six on the binary numeral system. Figure 2.1 (c) shows all
the quotients and remainders computed during the execution of Algorithm 2.

26 2 26 2 26 2
- 26 13 - 26 13 2 - 26 13 , 2
0 v(d02) = 0 - 12 6 v(d 02) = 0 - 12 6 2
q quotient
v(d12) = 1 v(d12) = 1 -6 3 | 2
remainder
22) = 0
v(d* -2 1 2
v(d32) =1 -0 0
v(d 42) =1

(a) (b) (c)

Figure 2.1: Example of applying Algorithm 2 to compute the sequence of digits


dm1-1 dm 2 • • • d1 d0 that represent value twenty six on base 2. (a) First iteration of
the algorithm. (b) Values of v(d0) and v(d1) computed after two iterations. (c) Final
result: 110102 = twenty six.

Figure 2.2 shows an example of applying Algorithm 2 to compute the sequence of


digits dm6-1dm6—2 • • • d16 d06 that represent value twenty six on base 16.
To convert a number from a positional numeral system with base A
to a number on a positional numeral system with base B, one may use
Equation 2.4 to compute the value of the number in base A and Algorithm 2
to compute the sequence of digits dm-1 dm—2 ••• dB cB that represent the
computed value on base B.
Representing numbers in the hexadecimal numeral system is more compact than
representing numbers in the binary numeral system. For example, the representation
of number one thousand in hexadecimal (3Е81б) requires only three digits while in
binary (111110 1 0002) it requires ten digits. Also, since both bases are power of two,

http://riscv-programming.org (Version: May 9, 2022) 10


2.2. REPRESENTING NUMBERS ON COMPUTERS

26 i 16 26 16
v(d 016) = 10 => d 016= "A"
- 16 1 - 16 1 | 16

10 v(d 016) =10 -0 0 v(d 116) = 1 => d 116= "1"


q quotient
v(d1i6) = 1 1A = twenty six
remainder

(a) (b) (c)

Figure 2.2: Example of applying Algorithm 2 to compute the sequence of digits


dm d16d1616 that represent value twenty six on base 16. (a) First iteration
dm—2 • • • 16
16—116
of the algorithm. (b) Values of v(d16) and v(d16) computed after two iterations. (c)
Final result: 1A16 = twenty six.

converting between these bases can be done by replacing subsets of consecutive four
bits by single hexadecimal digits, and vice versa. For example, Equation 2.8 illustrates
how number 3E816 can be converted to binary and Equation 2.9 shows how number
101101011101012 can be converted to hexadecimal.

3 E 8 = 0011111010002= 11111010002 (2.8)


|{z} |{z} |{z} 2
00112 111021000216

10 110101110101 = 2D75 16 (2.9)


|{-}|{Z-} !{^}|{^}
216 D16 716 516 2

2.2 Representing numbers on computers


A binary digit, or bit, is a digit in the binary numeral system, i.e., a digit that may
assume one of two values, “0” (zero) or “1” (one).
Most modern computers are built using digital electronic circuitry. These machines
usually represent information using voltage levels that are mapped to two states,
HIGH and LOW, or “1” (one) and “0” (zero). Hence, the basic unit of information
on modern computers is a bit. Consequently, numbers on computers are represented
by a sequence of binary digits, or bits.

2.2.1 Unsigned numbers


On computers, unsigned numbers are represented using the binary positional numeral
system with m bits. In other words, unsigned numbers are represented by a sequence
of m binary digits (or bits) d^1-1 dm-2 • • • d2 d0 and the value of the number is
defined by Equation 2.5.
Unsigned numbers are a subset of the natural numbers (N). The natural numbers
set has infinite numbers (zero to infinite), however, the set of unsigned numbers
that can be represented on computers depends on the amount of bits being used
to represent the number. For example, if only 8 bits are being used to represent
the number, then, only 256 numbers can be represented. In this case, the smallest
number is zero (000000002) and the largest one is 255 (111111112). The unsigned
numbers’ representation can represent natural numbers in the range [0 .. (2m — 1)]
with a sequence of m bits.
Typically, programming language types are mapped by the compiler to a native
type, i.e., a type known by the computer architecture. For example, on a 32-bit
computer, the “C” unsigned integer type is mapped by the compiler to a “32-bit
word”. In other words, on a 32-bit computer, an unsigned integer can represent
numbers from zero to 232 — 1. In this context, zero is represented by:

000000000 000000000 000000000 0000000002 (2.10)

http://riscv-programming.org (Version: May 9, 2022) 11


CHAPTER 2. DATA REPRESENTATION ON MODERN COMPUTERS

while 232 — 1 is represented by

11111111 11111111 11111111 111111112 (2.11)

To illustrate this concept, Table 2.3 shows the list of unsigned numbers that can
be represented with three-bit words.

Value in the
Three-bit word
unsigned representation
000 010
001 110
010 210
011 310
100 410
101 510
110 610
111 710

Table 2.3: Unsigned numbers that can be represented with three-bit words.

2.2.2 Signed numbers


On computers, signed numbers are a subset of the integer numbers (I). The set
of signed numbers usually include negative and non-negative numbers. While the
integer numbers set is infinite, the set of signed numbers that can be represented on
computers depends on the amount of bits being used to represent the number and on
the signed number representation. The most common signed number representation
is the “two’s complement”. The next sections presents the “signal and magnitude”,
the “one’s complement”, and the “two’s complement” representation.

Signal and magnitude

The “signal and magnitude” is a number representation that can be used to represent
signed numbers. In this representation, signed numbers are represented as a sequence
of m bits so that bit dm1-1 represents the signal of the number and the remaining
bits, i.e., bits dm-2 to d0, represent the magnitude. The magnitude value can be
computed using Equation 2.5. In this context, in case bit dm1-1 is “1”, then the
number is negative, otherwise, it is non-negative. Table 2.4 show the values of three-
bit words on the unsigned and signal and magnitude representations.

Value
Three-bit word signal and
unsigned
magnitude
000 010 010
001 110 110
010 210 210
011 310 310
100 410 —010
101 510 — 110
110 610 —210
111 710 -310

Table 2.4: Values of three-bit words on the unsigned and signal and magnitude rep­
resentations.

The “signal and magnitude” representation can represent numbers in the range
[—(2m-1 — 1) .. (2m-1 — 1)] with a sequence of m bits.

http://riscv-programming.org (Version: May 9, 2022) 12


2.2. REPRESENTING NUMBERS ON COMPUTERS

One’s complement

The “one’s complement” representation is a number representation that can be used


to represent signed numbers. In this representation, signed numbers are represented
as a sequence of m bits so that bit dm-1 represents the signal of the number. If bit
dm-1 is “1”, then the number is negative, otherwise, it is non-negative.
The magnitude of a non-negative number represented in one’s complement (i.e.,
a number in which dm1-1 = “0”) is computed in the same way the value of unsigned
numbers are computed, i.e., using Equation 2.5. For example, the three-bit number
“010” is a non-negative number in one’s complement representation and its magnitude
is two, since 0 x 22 + 1 x 21 + 0 x 20 = two.
The magnitude of a negative number represented in one’s complement (i.e., a
number in which dm1-1 = “1”) is computed by first “complementing” (inverting) all
bits and then using Equation 2.5. For example, the three-bit number “110” is a
negative number in one’s complement representation and its magnitude is one, since
the value of its complement (“001”) is one (0 x 22 + 0 x 21 + 1 x 20 = one).
Table 2.5 shows the values of three-bit words on the unsigned, signal and mag­
nitude, and one’s complement representations. Similar to the signal and magnitude
representation, there are two representations for value zero on the one’s complement
representation.

Value
Three-bit word signal and one’s two’s
unsigned
magnitude complement complement
000 010 010 010 010
001 110 110 110 110
010 210 210 210 210
011 310 310 310 310
100 410 —010 —310 —410
101 510 — 110 —210 —310
110 610 —210 — 110 —210
111 710 —310 —010 — 110

Table 2.5: Values of three-bit words on the unsigned, signal and magnitude, one’s
complement and two’s complement representations.

The “one’s complement” representation can represent numbers in the range [—(2m-1—
1) .. (2m-1 — 1)] with a sequence of m bits.

Two’s complement

The “two’s complement” representation is also a number representation that can


be used to represent signed numbers. Also, in this representation, signed numbers
are represented as a sequence of m bits so that bit dm1-1 represents the signal of the
number. If bit dm1-1 is “1”, then the number is negative, otherwise, it is non-negative.
The magnitude of a non-negative number represented in two’s complement (i.e.,
a number in which dm1-1 = “0”) is computed in the same way the value of unsigned
numbers are computed, i.e., using Equation 2.5. For example, the three-bit number
“010” is a non-negative number in two’s complement representation and its magnitude
is two, since 0 x 22 + 1 x 21 + 0 x 20 = two.
The magnitude of a negative number represented in two’s complement (i.e., a
number in which dm1-1 = “1”) is computed by first “complementing” (inverting)
all bits, then adding one, and, finally, using Equation 2.5. For example, the three-
bit number “110” is a negative number in two’s complement representation and its
magnitude is two, since the value of its complement (“001”) plus one is two.
Table 2.5 shows the values of three-bit words on the unsigned, signal and magni­
tude, one’s complement, and two’s complement representations. Notice that there is
only one representation for value zero on the two’s complement representation.

http://riscv-programming.org (Version: May 9, 2022) 13


CHAPTER 2. DATA REPRESENTATION ON MODERN COMPUTERS

Another interesting characteristic of the two’s complement representation is that


the addition, subtraction, and multiplication of two m-bit numbers in this represen­
tation are identical to the addition, subtraction, and multiplication operations of two
m-bit numbers in the unsigned binary representation3. As a consequence, the same
hardware may be used to perform these operations both on unsigned numbers and
signed numbers represented in “two’s complement”. Figure 2.3 shows an example in
which two numbers are added and the same sequence of bits is produced both in the
unsigned numbers representation and the two’s complement representation.

SequenceUnsigned Two’s complement


of bits representation representation
0102 210 210
+100 2 +410 +—410
1102 610 -210

Figure 2.3: The addition of two numbers produces the same sequence of bits both in
the unsigned numbers representation and the two’s complement representation.

The two’s complement representation can represent numbers in the range [— (2m-1) .. (2m-1-
1)] with a sequence of m bits.

NOTE: The two’s complement is the most common method to represent


signed integer numbers on modern computers.

2.2.3 Binary arithmetic and Overflow


Arithmetic on unsigned binary numbers is similar to arithmetic on unsigned decimal
numbers. When adding two unsigned binary numbers4, digits are added one by one,
from the least significant one (d0) to the most significant one (d0, 1), i.e., from the
rightmost digit to the leftmost digit. In some cases, the addition of two digits produces
a value that cannot be represented by a single digit. In these cases, a carry out is
produced and its value must be added to the next most significant digit. In the binary
representation, this situation occurs when the result is greater than one.
Figure 2.4 (a) illustrates the addition of two three-bit unsigned binary numbers.
First, the two least significant (rightmost) digits are added together. Since the result
(two=102) cannot be represented by a single binary digit, the least significant digit
of the result, i.e., zero, is placed in the current position and a carry is produced and
placed on top of the next digit (the red arrow illustrates this process). Then, the
second least significant digits (1 and 1) must be added. In this case, since there is a
carry from the previous digit addition, the carry is also added to both digits. The
result of this addition is three (11), hence, another carry is produced and placed on
top of the most significant digit. Again, the least significant digit of the result, i.e.,
one, is placed in the current position. Finally, the most significant digits are added
together with their carry and the result is one. Figure 2.4 (b) illustrates the same
addition using a simplified (cleaner) representation.
Subtraction of unsigned binary numbers is also performed in a similar way decimal
numbers are subtracted. When subtracting two unsigned binary numbers, digits are
subtracted one by one, from the least significant one (d0) to the most significant
one (dm1-1), i.e., from the rightmost digit to the leftmost digit. In some cases, the
subtraction is not possible because the first operand is smaller than the second one.

3This is only true if the output is represented using the same number of bits as the inputs (i.e.,
m bits) and if any overflow beyond these bits are discarded. This is usually the case in modern
computers.
4As discussed in previous section, adding (subtracting) two m-bit unsigned binary numbers pro­
duces the same sequence of m-bits as adding (subtracting) two m-bit signed numbers on the two’s
complement representation. Hence, the same approach used for unsigned binary numbers may be
used to perform addition and subtraction operations on signed numbers using the two’s complement
representation.

http://riscv-programming.org (Version: May 9, 2022) 14


2.2. REPRESENTING NUMBERS ON COMPUTERS

1 1 ■•“Carry digits 1 1 ■•“Carry digits


0 1 1 (3io) 0 1 1 (310)
+0 1 1 (310) +0 1 1 (3 10)
1 10 (610) 1 1 0 (610)
1
(a) (b)

Figure 2.4: Adding two three-bit binary numbers. (a) Red arrows indicate where the
carry out comes from. (b) Simplified representation (without arrows).

In these cases, “some value” is borrowed from the left digit and this borrowed value
must be accounted for when performing the subtraction on the left digit.
Figure 2.5 illustrates the subtraction of two three-bit unsigned binary numbers.
Figure 2.5 (b) shows the first step, in which the least significant digits are subtracted.
Since “1” cannot be subtracted from “0”, some value must be borrowed from the left
column. The “*” character indicates that value had to be borrowed from the left
column. The result in this column is “1”, since “10” (two) minus “1” (one) is “1”.
Figure 2.5 (c) illustrates the operation on the second least significant digit. Since
the right column borrowed from this column, the first operand is now “0”. Again,
since “1” cannot be subtracted from “0”, some value must be borrowed from the
left column. The result in this column is “1”, since “10” (two) minus “1” (one) is
“1”. Figure 2.5 (d) shows the subtraction of the most significant digits (zero minus
zero) and the final result. Figure 2.5 (e) shows a simplified representation of the
subtraction.
* * *

1 1 0 (610) 1 1 0 10 (610) 10 10 10 (610)


-0 1 1 (310) —0 1 1 (Зю) —0 1 1 (Зю)
1 1 1

(a) (b) (c)

* * * *

10 10 10 (610) 1 1 0 (610)
— 011 (310) — 011 (У
0 1 1 (Зю) 0 1 1 (Зю)

(d) (e)

Figure 2.5: Subtraction of two three-bit binary numbers. (a) The digits of both num­
bers are aligned on columns. (b) First, the least significant digits are subtracted - the
“*” character indicates that some value was borrowed from the left column. (c) The
second least significant digits are subtracted - again, the “*” character indicates that
some value was borrowed from the left column. (d) Finally, the most significant digits
are subtracted producing digit “0”. (e) Simplified representation of the subtraction.

2.2.4 Integer Overflow


An integer overflow occurs when the result of an arithmetic operation on two integer
m-bit binary numbers is outside of the range that can be represented by an m-bit
binary number. Figure 2.6 shows an example in which the addition of two three-bit

http://riscv-programming.org (Version: May 9, 2022) 15


CHAPTER 2. DATA REPRESENTATION ON MODERN COMPUTERS

unsigned binary numbers causes an integer overflow. In this case, adding one to seven
should result in eight, however, the value eight cannot be represented using only three
bits on the unsigned binary representation.

111 ^ carry digits


0 0 1 (110)
+ 1 1 1 (710)
000 (010)

Figure 2.6: Example of an integer overflow on the unsigned binary representation.


The result of one plus seven cannot be represented by a three-bit unsigned binary
number.

Even though the operation illustrated on Figure 2.6 characterizes an integer over­
flow on the unsigned binary representation, it does not characterize an integer overflow
on the signed (two’s complement) binary representation. In this case, the operation
is adding one (001) to minus one5 (111) and the expected result, i.e., zero, can be
represented by a three-bit unsigned binary number.
Figure 2.7 shows an example in which the addition of two three-bit signed binary
numbers using the two’s complement method causes an integer overflow. In this case,
however, there was not integer overflow on the unsigned binary number representation.
Notice that the result of the operation is as expected, i.e., four (100), on the unsigned
binary representation.

11 ^ carry digits
0 1 1 (310)
+ 001 (110)
1 0 0 (-410)

Figure 2.7: Example of an integer overflow on the signed binary representation. The
result of three plus one cannot be represented by a three-bit signed binary number
using the two’s complement representation.

2.3 Representing text


A character is the basic unit of information when representing text on computers and
usually corresponds to a letter (e.g., “a”), a decimal digit (e.g., “2”), a punctuation
mark (e.g., “.” or “?”), white spaces, or even a control information6.
The character encoding standard defines how characters are repre­
sented on computers. For example, the American Standard Code for Infor­
mation Interchange, or ASCII, defines that characters are represented by seven-bit
numbers. Table 2.6 shows a subset of the characters encoded by the American Stan­
dard Code for Information Interchange. Notice that the letter “a” is encoded as the
number 9710 (1100012) while digit “2” is encoded as the number 5010 (01100102).
The ASCII character encoding standard was designed in the 1960s and, even
though it included most symbols used on the English language, it lacked several
important symbols required by other languages, such letters with accents (e.g., “a”,
“c”, ...). In this context, several other character encoding standards were introduced,
including an extension to the ASCII standard, the “Extended ASCII”, or EASCII.
Accordingly to Google7, in 2008 the UTF-8 character encoding standard became the
most common encoding for HTML files. As of 2020, a survey performed by the

5Notice that the three-bit sequence 111 represents the value minus one on the two’s complement
representation.
6Control characters are not intended to represent printable information. A line feed, carrier return
and backspace are examples of control characters on computers.
7https://googleblog.blogspot.com/2008/05/moving-to-unicode —51.html

http://riscv-programming.org (Version: May 9, 2022) 16


2.3. REPRESENTING TEXT

Binary Hex. Dec. Char. Binary Hex. Dec. Char.


... 10000012 4116 65 10 A
01000012 2116 3310 ! 10000102 4216 66 10 B
01000102 22 16 3410 ” ...
... 10110012 5916 89 10 Y
01011002 2C16 4410 , 10110102 5A16 90 10 Z
01011012 2D16 45 10 - ...
01011102 2E 16 4610 . 11000012 6116 97 10 a
01011112 2F16 4710 / 11000102 6216 98 10 b
01100002 30 16 4810 0 ...
01100012 3116 4910 1 11110012 7916 12110 y
01100102 32 16 5010 2 11110102 7A16 12210 z
... ...
01110002 38 16 5610 8 11111002 7C16 12410 |
01110012 39 16 5710 9 11111012 7D16 12510 }
... 11111102 7E16 12610 ~

Table 2.6: Subset of the characters encoded by the ASCII character encoding stan­
dard. Hex. and Dec. columns show the encoding value in hexadecimal and decimal
representation while the Char. column shows the symbol encoded by the character.

W3Techs web site8 indicated that more than 95.5 % of the world wide web websites
are encoded with the UTF-8 character encoding standard.
The “Unicode (or Universal Coded Character Set) Transformation Format - 8-
bit”, or UTF-8 for short, is a variable-width character encoding standard. In this
standard, each character may be represented by one, two, three, or four bytes, i.e.,
one, two, three, or four 8-bit numbers. Common characters, such as letters “a”, “b”,
and “c”, are represented by a single byte, while more exotic ones are represented
using multiple bytes. The euro currency sign (€), for example, is encoded using three
bytes: 111000102, 100000102, and 101011002.
The UTF-8 standard was designed to be backward compatible with the ASCII
standard. Hence, ASCII characters are represented on the UTF-8 standard using a
single byte with the the same value. For example, letter “a” is represented by value
ninety seven in both standards. In this way, a software designed to work with the
UTF-8 standard can naturally open and handle ASCII encoded files.
Texts are represented in computers as sequences of characters on mem­
ory. For example, the word “Yes” is represented by a sequence of three characters
(“Y”, “e”, and “s”) stored on consecutive memory positions. In case the ASCII
character encoding standard is being used, the three consecutive memory positions
will contain values 12110, 10110, and 11510, respectively. Figure 2.8 illustrates how
the word “maca”9 is represented in three different character encoding standards: the
UTF-8, the ISO-LATIN-1 and the Mac OS Roman. Each square represents a byte
and the values inside the squares are in hexadecimal. Notice that the UTF-8 standard
requires two bytes to represent letter “c” and two bytes to represent letter “a”.

M a 9 a M a 9 a M a a
4D 61 E7 E3 4D 61 8D 8B 4D 61 C3 A7 C3 A3
ISO-LATIN-1 Mac OS Roman UTF-8
Figure 2.8: Word “maca” represented in three different character encoding standards:
the UTF-8, the ISO-LATIN-1 and the Mac OS Roman.

8https://w3techs.com/technologies/overview/character_encoding
9 “Maca” is the word for apple in Portuguese.

http://riscv-programming.org (Version: May 9, 2022) 17


CHAPTER 2. DATA REPRESENTATION ON MODERN COMPUTERS

2.4 Organizing data on the memory


This section discusses how information is organized on the computer main memory.
The discussion focus on byte addressable memories, which is the most common type
of main memory used on modern computers.

2.4.1 Texts on the main memory


As discussed in Section 2.3, texts are represented in computers as sequences of charac­
ters on memory. The sequence is stored on consecutive memory words, i.e., memory
words with consecutive addresses.
ASCII characters are encoded using seven bits, however, when stored on a byte
addressable memory, each character is usually stored in a single memory word, i.e.,
they occupy eight bits1011. Consequently, a five character text (e.g., “hello”) encoded
with the ASCII standard is stored on five consecutive memory words.
UTF-8 characters are encoded using one, two, three, or four bytes, hence, when
stored on a byte addressable memory, each character may require one, two, three,
or four memory words. As illustrated by Figure 2.8, the UTF-8 character encoding
standard requires six bytes to represent the word “maca”. Consequently, it requires
six memory words to represent this word on a byte addressable memory.
In programming languages, the term “string” is often used to denote a sequence
of characters. A NULL terminated string is a sequence of characters terminated by
the character NULL, which is represented by value zero on most character encoding
standards. A “C” string, i.e., a string on the “C” programming language, is a NULL
terminated string. Hence, in “C”, the string “yes” takes four memory words, three
to store the letters “y”, “e”, and “s”, and another one to store the NULL character.
The following program shows two different ways of declaring and initializing a string
in a “C” program. The first approach (line 2), uses a sequence of symbols between
quotes while the second one (line 3) uses an sequence of hexadecimal values using the
array notation.

1 #include<stdio.h>
2 char name1[] = "John";
3 char name2[] = {0x4a, 0x6f, 0x68, 0x6e, 0x00};
4 int main()
5 {
6 printf("Name 1: \"%s\"\n", namel);
7 printf("Name 2: \"%s\"\n", name2);
8 printf("Size of name 1 = %d\n", sizeof(name1));
9 printf("Size of name 2 = %d\n", sizeof(name2));
10 return 0;
11 }

Both strings (name1 and name2) in previous code require five memory words to
be stored on memory11. In fact, since the hexadecimal values used in line 3 are
the values for the “J”, “o”, “h”, and “n” letters on the ASCII and UTF-8 encoding
standards, both string are identical. The following listing shows the output of the
previous program.

1 Name 1: "John"
2 Name 2: "John"
3 Size of name 1=5
4 Size of name 2=5

10In this case, the eighth bit is always set as zero.


11 Notice that the string “John” is terminated by a NULL character in “C”.

http://riscv-programming.org (Version: May 9, 2022) 18


2.4. ORGANIZING DATA ON THE MEMORY

2.4.2 Numbers on the main memory


As discussed in Section 2.2, numbers on computers are represented by a sequence of
m bits. In case m is greater than the memory word size, then, the sequence of m
bits must be split and stored on multiple consecutive memory words. For example,
a 32-bit number is split in four 8-bit parts and stored on four consecutive memory
words on a byte addressable memory.
The endianness format refers to the order in which the bytes are stored on a
computing system memory. There are two common formats: little-endian and big-
endian. The little-endian format places the least significant byte (LSB) on the memory
position associated with the lowest address while the big-endian format places the
least significant byte on the memory position associated with the highest address.
Figure 2.9 illustrates how the 32-bit number 00000000 00000000 00000100 000000012
(102510) can be stored on a byte addressable memory starting on address 000 in
both formats. Notice that in the little-endian format (Figure 2.9 (a)), the least
significant byte (000000012) is stored in address 000 while in the big-endian format
(Figure 2.9 (b)), the least significant byte (000000012) is stored in address 003.

Address Contents Address Contents


000 000000012 ^ LSB 000 000000002
001 000001002 001 000000002
002 000000002 002 000001002
003 000000002 003 000000012 ^ LSB

(a) little-endian (b) big-endian

Figure 2.9: 32-bit number 00000000 00000000 00000100 000000012 (102510) stored on
four consecutive memory words using the (a) little-endian and the (b) big-endian
endianness formats.

2.4.3 Arrays on the main memory


In programming languages, an array is a systematic arrangement of similar objects
in which each object is identified by an index. A one-dimensional array, a.k.a. a
vector, is an array in which the objects, or the array elements, are identified by a one­
dimensional index. The following “C” code shows a vector (V) that contains four int
elements and a function that prints the first and the last element of the vector. Notice
that the first element is associated with index zero while the last one is associated
with index two12.

i int V[] = {9, 8, 1};


2 void print_V()
3 {
4 printf("First element = %d\n", V[0]);
5 printf("Last element = %d\n", V[2]);
6 }

Vector elements are usually organized in a linear fashion on the memory. Hence,
when translating the previous code into machine language, all elements (int values) of
vector V are placed in consecutive memory positions - starting with the first element,
i.e., V[0]. The base address of an array is the address of the first memory
word that is being used to store the array elements. Assuming the base address
of vector V is 000, then the first element (V[0]) is stored starting at memory address
000. Also, assuming each element requires four memory words13, the second element
12This is a property of the “C” programming language. Other languages, such as Pascal, associate
the first element with index one.
13The “C” int type is used to represent integer numbers and is usually mapped to 32-bit signed
numbers.

http://riscv-programming.org (Version: May 9, 2022) 19


CHAPTER 2. DATA REPRESENTATION ON MODERN COMPUTERS

is stored starting at memory address 004 and the third one starting at memory address
008. Figure 2.10 illustrates the contents of vector V placed on memory starting at
address 000. Notice that, in this example, each element is a 32-bit number that is
stored on four consecutive memory words using the little-endian format.

Address Contents
000 000010012
001 000000002
■v[0] = 910
002 000000002
003 000000002
004 000010002
005 000000002
• v[1] = 810
006 000000002
007 000000002
008 000000012
009 000000002
■v[2] = 110
010 000000002
011 000000002

Figure 2.10: Elements of vector V stored on memory starting at address 000.

The previous example showed an array of int elements. Nonetheless, in “C”, the
programmer may also create arrays of other types. For example, one may create an
array of char, in which each element occupies only one byte, an array of double, in
which each element occupies 8 bytes, or even an array of a new type defined with the
struct operator, in which each element may occupy several bytes.
Let:

• Vaddr be the base address of a vector V;

• elemsize be the size of each element of V in bytes;

• V[i] be the ith element of the vector;

In “C”, and several other programming languages, each element V[i] occupies
elem size memory words of a byte addressable memory and is placed at the main
memory starting at address &V[i], which is defined by Equation 2.12.

&V[i] = Vaddr + i x elemnsize (2.12)


A multi-dimensional array is an array in which each element is identified by a
multi-dimensional index. The following “C” code shows a two-dimensional array (M)
that contains six int elements and a function that prints two elements of the array.
In this example, each element is associated with a unique two-dimensional index [x][y]
so that x G [0 . . 1] and y G [0 .. 2].

i int M[][] = { {7, 9, 11},


2 {2, 8, 1} };
з void print_M()
4 {
5 printf("Element M[0][0] = %d\n", M[0][0]);
6 printf("Element M[1][2] = %d\n", M[1][2]);
7 }

Two-dimensional arrays are commonly used to represent matrices. In this context,


the first part of the index ([x]) is often used to identify the row and the second part
([y]) is used to identify the column. Hence, the element located at the first row and
last column in previous example is identified by index M[0][2].

http://riscv-programming.org (Version: May 9, 2022) 20


2.4. ORGANIZING DATA ON THE MEMORY

The way two-dimensional arrays are organized on memory depends on the pro­
gramming language. In “C”, elements are grouped by row and each row is placed on
memory consecutively. Hence, in the previous example, the elements of the first row,
i.e., M[0][0]=7, M[0][1]=9, and M[0][2]=11, are placed first on memory. Then, the
elements of the second row, i.e., M[1][0]=2, M[1][1]=8, and M[1][2]=1, are placed
after the elements of the first row. This way of organizing two-dimensional arrays on
memory is known as row-major order.
Let:

• A be a M x N two-dimensional array in “C”, i.e., an array in which the x G


[0 . . M - 1] and y G [0 .. N - 1].

• Aaddr be the base address of array A;

• elemsize be the size of each element of A in bytes;

• A[x][y] be the array element associated with index [x][y];

In “C”, each element A [x][y] occupies elemsize memory words of a byte addressable
memory and is placed at the main memory starting at address &A[x][y], which can
be computed using Equation 2.13.

&A[x][y] = Aaddr + x x elemsize x N + y x elemsize . (2.13)


1-------------- {-------------- } |--------- {--------- }
offset 1 offset 2

Notice that Equation 2.13 adds to the base address (Aaddr) two offsets: offset 1
and offset 2. The first offset is the amount of space in bytes required to store all
elements that belong to previous rows, i.e., rows that must be placed before row x.
The second offset is the amount of space in bytes required to store all elements that
belong to the same row but must be placed before element A[x][y], i.e., the elements
that has a column index less than y.

NOTE: The row-major order is used in the following programming lan­


guages: “C”, “C++”, “Objective-C”, “PL/I”, “Pascal”, and other.
Some programming languages, such as “Fortran”, “MATLAB”, “GNU Oc­
tave”, “R”, “Julia”, and other, organize two-dimensional arrays on memory
using the column-major order. In this case, elements of a two-dimensional
array are organized in memory column by column, i.e., the elements of the
first column are placed first then the elements of the second column are placed
after the elements of the first one and so on.

2.4.4 Structs on the main memory


In “C”, structs are data types defined by the user in which data items of different
types may be grouped and combined in a single data type. The following “C” code
shows an example in which an int, an one-dimensional array with 255 char elements
and a short data item are combined in a struct named user id to form a new data
type. Notice that each data item inside the struct is identified by a name, the first
one by id, the second one by name, and the third one by level. These items are also
known as the “fields” of the struct data type.

i struct user_id {
2 int id;
з char name[256];
4 short level;
5 };
6

7 struct user_id manager;


8

http://riscv-programming.org (Version: May 9, 2022) 21


CHAPTER 2. DATA REPRESENTATION ON MODERN COMPUTERS

9 void print_manager()
10 {
ii printf("Manager id = %d\n", manager.id);
12 printf("Manager name = %s\n", manager.name);
13 printf("Manager level = %d\n", manager.level);
14 }

Assuming each int/char/short item requires four/one/two bytes to be stored on


memory, the struct data type defined in the previous example requires 262 bytes to
be stored on memory: four bytes to store the id field, 256 bytes to store the name
field and two bytes to store the level field.
All fields of a single struct item are stored sequentially on memory in the same
order they appear on the declaration. Hence, in the previous example, the field id is
placed first, then field name is placed next and, finally, field level is placed last.
The base address of an struct is the address of the first memory word
that is being used to store the fields of the struct. Assuming the base address
of variable manager in previous example is 000, the field id is placed on addresses
000 to 003, field name is placed on addresses 004 to 259, and field level is placed
on addresses 260 and 261. Figure 2.11 illustrates the contents of variable manager
placed on memory starting at address 000.

Address Contents
000 000000012
001 000000002
■ id
002 000000002
003 000000002
004 010010102 n name[0]

005 011011112 n name[1]

... ...
259 000000002 * name[255]

260 000000002
• level
261 000001112

Figure 2.11 : Elements of variable manager stored on memory starting at address 000.

2.5 Encoding instructions


Computer instructions are usually encoded as a sequence of bits. The number of bits
required to encode each instruction varies accordingly to the computer architecture.
For example, at one hand, the RISC-V instruction set architecture defines that all
RV32I instructions are encoded using a sequence of 32 bits. On the other hand,
instructions that belong to the x86 instruction set architecture family are encoded
with a varying number of bits. Figure 2.12 illustrates how two different instructions,
that belong to two different instruction set architectures, are encoded.
The 16 bits of the x86 instruction illustrated on Figure 2.12 (a) are organized in
four fields: opcode, mod, op1 and op2. The opcode, or operation code, field contains
a value that is used by the computer to identify the instruction. In this case, the value
100010012 indicates that this is a mov instruction. The other fields, mod, op1 and
op2, are the instruction parameters. In this case, they specify that the mov operation
must be performed between registers %esp and %ebp, which are indicated by values
1002 and 1012 on fields op1 and op2.
The 32 bits of the RISC-V instruction illustrated on Figure 2.12 (b) are organized
in six fields: funct7, rs2, rs1, funct3, rd, and opcode. The funct7, funct3, and opcode
fields contains values that is used by the computer to identify the instruction and,
hence, the operation that must be performed. In this case, these values indicate
that this is an add instruction. The other fields, rs2, rs1, and rd, are the instruction

http://riscv-programming.org (Version: May 9, 2022) 22


2.5. ENCODING INSTRUCTIONS

Opcode op1 funct7 rs1 rd


(mov) (%esp) (add) (x1) (x3)

10001001 11 100 1012 0000000 00000 00001 000 00011 01100112


Mod (%ebp) (x0) (add) (add)
op2 rs2 funct3 opcode

mov %esp, %ebp add x3, x1, x0


(a) (b)

Figure 2.12 : Encoding of two different instructions: (a) instruction mov, which belongs
to the x86 instruction set architecture, and (b) instruction add, which belongs to the
RISC-V instruction set architecture.

parameters. These parameters specify that the add operation must be performed
using the values stored in registers x1 and x0 and the result stored in register x3,
which are identified by values 00012 , 00 002, and 00112 on fields rs1, rs2, and rd,
respectively.
Most modern computers store the code, i.e., the program instructions, on the same
memory they store the data - the main memory. Also, modern computer instructions
are encoded using multiples of 8 bits so that they fit the size of multiple memory words
on a byte addressable memory. Figure 2.13 shows an example of how a program
written in x86 assembly language is mapped to machine language and stored on a
byte addressable memory. Notice that, the first instruction, push $ebp, is encoded
using one byte while the third one, imul $113, 12(%ebp), %eax, is encoded using
four bytes. Also, notice that instructions are placed sequentially on memory, in the
same order they appear on the original assembly program.

Machine language (x86)


stored on memory
Contents Address
010101012 000
100010012 001
111001012 002
011010112 003
Assembly language (x86)
010001012 004
funcl:
push %ebp . 000011002 005
mov %esp, %ebp
imul $113, 12(%ebp), %eax 011100012 006
add 8(%ebp), %eax 000000112 007
imul 16(%ebp), %eax
%ebp 010001012 008
pop
ret 000010002 009
000011112 010
101011112 011
010001012 012
000100002 013
010111012 014
110000112 015

Figure 2.13 : Mapping x86 instructions from an assembly language program to mem­
ory.

http://riscv-programming.org (Version: May 9, 2022) 23


Chapter 3

Assembly, object, and


executable files

This chapter presents the main concepts and elements of assembly, object, and exe­
cutable files.

3.1 Generating native programs


In this section, we discuss how a program written in a high-level language, such as C,
is translated into a native program.
As discussed in Chapter 1, a native program is a program encoded us­
ing instructions that can be directly executed by the computer hardware,
without help from an emulator or a virtual machine. These programs are
usually automatically translated from programs written in high-level languages, such
as C, by tools like compilers, assemblers, and linkers.
A program written in a high-level language, such as C, is encoded as a plain text
file. High-level languages are designed to be agnostic of ISA and they are composed of
several abstract elements, such as variables, repetition, or loop statements, conditional
statements, routines, etc.. The following code shows an example of a program written
using the C language, which is a high-level language.

i int main()
2 {
з int r = func (10)
4 return r+1;
5 }*

A compiler is a tool that translates a program from one language to


another. Usually, programming language compilers are employed to translate pro­
grams written in high-level languages into lower-level languages. For example, a C
compiler is employed to translate a program written in C language into assembly lan­
guage. The GNU project C and C++ compiler1, or gcc, is a compiler that is capable
of translating programs written in C and C++ languages into assembly programs,
i.e., a program written in assembly language. The following command line illustrates
how the riscv64-unknown-elf-gcc tool, a GNU project C and C++ compiler that
produces code for RISC-V based computers, can be invoked to produce a RV32I as­
sembly program from a C program. In this example, the C program is stored on the
main.c file and the RV32I assembly program will be stored on the main.s file.

$ riscv64-unknown-elf-gcc -mabi=ilp32 -march=rv32i -S main.c -o main.s

1https://gcc.gnu.org/

24
3.1. GENERATING NATIVE PROGRAMS

An assembly program is also a program encoded as a plain text file. The following
code shows an example of a program written using the RV32I assembly language.
This program has the same semantics as the previous C program.

i .text
2 .align 2
з main:
4 addi sp,sp,-16
5 li a0,10
6 sw ra,12(sp)
7 jal func
8 lw ra,12(sp)
9 addi a0,a0,1
10 addi sp,sp,16
11 ret

Different from high-level languages, assembly language is very close to the ISA.
For example, the previous assembly program contains references to instructions (e.g.,
addi, li, ...) and registers (e.g., sp, ra, a0) that belong to the RV32I ISA. Lines
4 to 11 of the previous code contain assembly instructions, which are converted by
the assembler into RV32I machine instructions. As a consequence, they are ISA
dependent, i.e., an assembly program generated for one ISA is usually not compatible
with other ISAs.
Machine language is a low-level language that can be directly processed
by a computer’s central processing unit (CPU). An assembler is a tool
that translates a program in assembly language into a program in machine
language. For example, it converts assembly instructions (encoded as sequences of
ASCII characters) into machine instructions (encoded as sequences of bits accordingly
to the ISA). Each assembly language is associated with a given ISA.
The “GNU Assembler”2 tool, or as, is an assembler that is capable of translat­
ing programs written in several assembly languages into machine language for their
respective ISAs. In this book we will use the as tool to translate RV32IM assembly
programs to machine language programs. The following command line illustrates how
the riscv64-unknown-elf-as tool, a version of the GNU Assembler that generates
code for RISC-V ISAs, can be invoked to assemble a RV32I assembly program. In
this example, the RV32I assembly program is stored on the main.s file and the result,
a file that contains code in machine language, will be stored on the main.o file.

$ riscv64-unknown-elf-as -mabi=ilp32 -march=rv32i main.s -o main.o

Assemblers usually produce object files that are encoded in binary and contains
code in machine language. The object file also contains other information, such as
the list of symbols (e.g., global variables and functions) defined in the file. There
are several known file formats used to encode object files. The Executable and
Linking Format, or ELF, is frequently used on Linux-based systems while the
Portable Executable format, or PE, is used on Windows-based systems. The
riscv64-unknown-elf-as tool, used in the previous example, produces an ELF-based
object file.
Even though the object file produced by the assembler contains code in machine
language, it is usually incomplete in the sense that it may still need to be relocated
(more on relocation later) or linked with other object files to compose the whole
program. For example, the code in an object file may need to be linked with the C
library so that the program can invoke the printf function. As a consequence, the
object file produced by the assembler is not an executable file.
A linker is a tool that “links” together one or more object files and
produces an executable file. The executable file is similar to an object file in the

2https://www.gnu.org/software/binutils/

http://riscv-programming.org (Version: May 9, 2022) 25


CHAPTER 3. ASSEMBLY, OBJECT, AND EXECUTABLE FILES

High-level language program


(C, C++, Java, Pascal, ...)
(text file)

Assembly language program


(text file)

Machine language program


(object file - binary)

Linker (e.g. Id)

01010101 Machine language program


10001001 (executable file - binary)

Figure 3.1: Native code generation flow.

sense that it is encoded in binary and contains code in machine language. Nonetheless,
it contains all the required elements (e.g., libraries) for execution.
The following command line illustrates how the riscv64-unknown-elf-ld tool,
a version of the GNU Linker3 tool that links object files generated for RISC-V ISAs,
can be invoked to link two object files together: the main.o and mylib.o object files.
In this example, the linker will produce an executable file named main.x.

$ riscv64-unknown-elf-ld -m elf32lriscv main.o mylib.o -o main.x

Figure 3.1 illustrates the code generation process used to produce a native program
executable file from a C program organized in two files. First, the two C program files
are translated into assembly programs by the compiler. Then, the assembly programs
are assembled by the assembler, which produces object files. Finally, the linker links
the object files together producing an executable file.
Assuming the high-level language program files are named main.c and func.c,
the following sequence of commands produce a RV32I executable file named main.x.

$ riscv64-unknown-elf-gcc -mabi=ilp32 -march=rv32i -S main.c -o main.s


$ riscv64-unknown-elf-as -mabi=ilp32 -march=rv32i main.s -o main.o
$ riscv64-unknown-elf-gcc -mabi=ilp32 -march=rv32i -S func.c -o func.s
$ riscv64-unknown-elf-as -mabi=ilp32 -march=rv32i func.s -o func.o
$ riscv64-unknown-elf-ld -m elf32lriscv main.o func.o -o main.x

3.1.1 Inspecting the contents of object and executable files


Object and executable files are encoded as binary files, hence, it is not easy to look at
their contents directly. To inspect their contents, developers usually rely on programs
that decode and translate their information to a human-readable representation, usu­
ally a textual format. There are several tools that can be used to inspect the contents
of object and executable files. The objdump, nm, and readelf tools are examples of
tools that can be used to inspect the contents of object and executable files on Linux­
based systems. The following sections show several examples of how these tools can
be used to inspect the contents of object and executable files.

3https://www.gnu.org/software/binutils/

http://riscv-programming.org (Version: May 9, 2022) 26


3.2. LABELS, SYMBOLS, REFERENCES, AND RELOCATION

3.2 Labels, symbols, references, and relocation


3.2.1 Labels and symbols
Labels are “markers” that represent program locations. They are usually
defined by a name ended with the suffix “:” and can be inserted into an assembly
program to “mark” a program position so that it can be referred to by assembly
instructions or other assembly commands, such as directives.
The following code shows an assembly program that contains two labels: x:,
defined in line 1, and sum10:, defined in line 4. The x: label identifies a program
location that contains a variable, which is allocated and initialized by the directive
.word 10 (line 2). The sum10: label identifies the program location that contains
the first instruction of the sum10 routine, in other words, it defines the routine entry
point. Also, in this example, the x: label is used in instruction Iw (line 5) to refer to
variable x.

i x:
2 .word 10
3

4 sum10:
5 Iw a0, x
6 addi a0, a0, 10
7 ret

Global variables and program routines are program elements that are stored on
the computer main memory. Each variable and each routine occupies a sequence of
memory words and are identified by the address of the first memory word they occupy.
At one hand, to read the contents of a global variable, or execute a routine, it suffices
to have their addresses, i.e., the address of the first memory word they occupy4 . On
the other hand, the addresses assigned to variables and routines are only final on the
executable file, after the linker links together the multiple object files into a single
file. Hence, assembly programs require a mechanism to refer to global variables and
routines. This is accomplished by using labels, as illustrated in the previous example.
In this context, before allocating space for each global variable or producing the code
for each routine, the programmer (or the compiler) defines a label that will be used
to identify the variable or the routine.
Program symbols are “names” that are associated with numerical val­
ues and the “symbol table” is a data structure that maps each program
symbol to its value. Labels are automatically converted into program symbols by
the assembler and associated with a numerical value that represents its position in
the program, which is a memory address. The assembler adds all symbols to the
program’s “symbol table”, which is also stored on the object file.
We can inspect the contents of the object file by using tools that decode the infor­
mation on the object file and shows them on a human-readable format, i.e., a textual
format. The GNU nm tool, for example, can be used to inspect the “symbol table”
of an object file. Assuming the previous code was encoded into an object file named
sumlO.o, we can inspect its symbol table by executing the riscv64-unknown-elf-nm
tool as follows.

$ riscv64-unknown-elf-nm sumlO.o
00000004 t .L0
00000004 t sum10
00000000 t x

4To execute a routine it suffices to set the PC with the address of the first instruction of the
routine - this is usually done by executing a “jump” instruction, which sets the PC with a given
value.

http://riscv-programming.org (Version: May 9, 2022) 27


CHAPTER 3. ASSEMBLY, OBJECT, AND EXECUTABLE FILES

Notice that, in this case, the symbol table contains three symbols: .L05, sum10,
and x, which are associated with values 00000004, 00000004, and 00000000, respec­
tively.
The programmer may also explicitly define symbols by using the .set directive.
The following code shows a fragment of assembly code that employs the .set directive
to define a symbol named answer and assign value 42 to it.

i .set answer, 42
2 get_answer:
з li a0, answer
4 ret

Assuming the previous code is stored on a program file named get answer.s, we
can assemble it and inspect the object file symbol table by executing the following
commands:

$ riscv64-unknown-elf-as -march=rv32im get_answer.s -o get_answer.o


$ riscv64-unknown-elf-nm get_answer.o
0000002a a answer
00000000 t get_answer

Notice that the symbol table contains two symbols: answer and get answer. The
answer symbol is an absolute symbol, i.e., its value is not changed during the linking
process - this is indicated by the letter ‘a’ on the output. The get answer symbol
is a symbol that represents a location on the .text section and may have its value
(which is an address) changed during the relocation process. The next sections discuss
the relocation process and the program sections’ concept.

3.2.2 References to labels and relocation


Each reference to a label must be replaced by an address during the assembling and
linking processes. For example, in the previous code, the reference to label x:, in
instruction lw (line 5), is replaced by address 0, i.e., the address of the variable
represented by label x, when assembling the program.
To illustrate this concept, let us consider the following RV32I assembly program,
which contains four instructions and two labels. The first label (trunk42 - line 1)
identifies the entry point of a function while the second one (done - line 5) identifies
a program location that is the target of a branch instruction56, which is displayed in
line 3.

i trunk42:
2 li t1, 42
з bge t1, a0, done
4 mv a0, t1
5 done:
6 ret

When assembling this program, the assembler translates each assembly instruction
(e.g., li, bge, ...) to a machine instruction, i.e., an instruction encoded with 32
bits. As a result, the program occupies a total of 16 memory words, four for each
instruction. Also, the assembler maps the first instruction to address 0, the second
one to address 4, and so on. In this context, the trunk42 label, which marks the
5 The .L0 symbol was automatically introduced by the assembler when translating the lw a0, x
assembly instruction. This is a special instruction called pseudo-instruction that will be discussed
latter on Section 6.4.
6A branch instruction is an instruction that change the execution flow under certain conditions
- In this example, the bge t1, a0, done (branch greater equal) instruction jumps to the position
identified by the done label if the value in register t1 is greater or equal to the value in register a0.

http://riscv-programming.org (Version: May 9, 2022) 28


3.2. LABELS, SYMBOLS, REFERENCES, AND RELOCATION

beginning of the program, is associated with address 0 and the done label, which
marks the position in which instruction ret is located, is associated with address c.
Since the bge instruction has a reference to label done, the assembler encodes the
address associated with the done label (address c) in the fields of this instruction.
The GNU objdump tool can be used to inspect several parts of the object file.
The following example shows how to use the riscv64-unknown-elf-objdump7 tool
to decode the data and instructions on the trunk.o file so that we can inspect its
contents.

$ riscv64-unknown-elf-objdump -D trunk.o

trunk.o: file format elf32-littleriscv

Disassembly of section .text:

00000000 <trunk42>:
0: 02a00313 li t1,42
4: 00a35463 bge t1,a0,c <done>
8: 00030513 mv a0,t1

0000000c <done>:
c: 00008067 ret

Notice that, for each instruction, it shows its address, its encoding in hexadeci­
mal, and a text that resembles assembly code8. The bge t1, a0, done instruction,
for example, is mapped to address 4 and encoded with the 32-bit value 00a35463.
The objdump tool indicates that it refers to the label done, which is mapped to ad­
dress c (bge t1,a0,c <done>). Also, notice that the labels (and their addresses) are
displayed on their respective program position.
In the previous example, the trunk42 function starts at address 0, however, when
linking this object file (trunk.o) with others, the linker may need to move the code
(assign new addresses) so that they do not occupy the same addresses. In this process,
the addresses associated with labels may change and each reference to a label must
also be fixed to reflect the new addresses.
Relocation is the process in which the code and data are assigned new
memory addresses. As discussed previously, during the relocation process, the
linker needs to adjust the code and data to reflect the new addresses. More specifically,
the addresses associated with labels on the symbol table and the references to labels
must be adjusted. The relocation table is a data structure that contains
information that describes how the program instructions and data need to
be modified to reflect the addresses reassignment. Each object file contains
a relocation table and the linker uses their information to adjust the code when
performing the relocation process.
The following example shows how to use the riscv64-unknown-elf-objdump tool
to inspect the contents of the relocation table on the trunk.o file. Notice that, in this
case, the object file contains one relocation record, which indicates that the instruction
on address 4, a RISC-V branch instruction, contains a reference to label done. The
linker uses this information to adjust the branch instruction when the done label is
assigned a new address.

$ riscv64-unknown-elf-objdump -r trunk.o

trunk.o: file format elf32-littleriscv

7The -D option instructs the riscv64-unknown-elf-objdump tool to disassemble the contents of


the object file.
8Evcn though the syntax is similar, the code showed by the objdump tool is not assembly code.

http://riscv-programming.org (Version: May 9, 2022) 29


CHAPTER 3. ASSEMBLY, OBJECT, AND EXECUTABLE FILES

RELOCATION RECORDS FOR [.text]:


OFFSET TYPE VALUE
00000004 R_RISCV_BRANCH done
The following example shows what happens to the previous program once it is
linked. First, we produce the trunk.x file by invoking the linker. Then, we inspect
the contents of the trunk.x file by using the riscv64-unknown-elf-objdump tool.

$ riscv64-unknown-elf-ld -m elf32lriscv trunk.o -o trunk.x


$ riscv64-unknown-elf-objdump -D trunk.x

trunk.x: file format elf32-littleriscv

Disassembly of section .text:

00010054 <trunk42>:
10054: 02a00313 li t1,42
10058: 00a35463 bge t1,a0,10060 <done>
1005c: 00030513 mv a0,t1

00010060 <done>:
10060: 00008067 ret

Notice that the code on the trunk.x program was relocated, i.e., assigned new
addresses. In this example, the code of the trunk42 routine starts at address 10054
and the bge instruction jumps to address 10060 in case the value in register t1 is
greater or equal to the value in register a0.

3.2.3 Undefined references


As discussed in previous sections, assembly code relies on labels to refer to program
locations. In some cases, an assembly code refers to a label that is not defined in the
same file. This is common when invoking a routine that is implemented on another
file or when accessing a global variable that is declared on another file. The following
example shows an assembly code that refers to a label that is not defined on the same
file, called main.s. The exit label is used on instruction jal (line 4), however, it is
not defined in this file9.

i # Contents of the main.s file


2 start:
з li a0, 10
4 li a1, 20
5 jal exit

The assembler assembles this program and register the exit label on the symbol
table as an undefined symbol. The riscv64-unknown-elf-nm tool identifies the un­
defined symbols by placing the ‘U’ character before the symbol name. Assuming the
previous code was assembled into the main.o object file, we can inspect the contents
of its symbol table as follows:

$ riscv64-unknown-elf-nm main.o
00000000 t start
U exit
The assembler also register the reference to this symbol on the relocation table.
The riscv64-unknown-elf-objdump tool shows that the main.o object file includes
a relocation record for the reference to the exit label on the jal instruction.
9The jal instruction is used to invoke routines. In this case, it is invoking the exit routine. This
instruction will be further discussed in Section 6.7.3.

http://riscv-programming.org (Version: May 9, 2022) 30


3.2. LABELS, SYMBOLS, REFERENCES, AND RELOCATION

$ riscv64-unknown-elf-objdump -r main.o

main.o: file format elf32-littleriscv

RELOCATION RECORDS FOR [.text]:


OFFSET TYPE VALUE
00000008 R_RISCV_JAL exit

When linking the object files, the linker must resolve the undefined symbols, i.e.,
it must find the symbol definition and adjust the symbol table and the code with
the symbol value. In the previous example, the linker will look for the exit symbol
so that it can adjust the jal instruction to refer to the correct address. In case it
cannot find the definition of the symbol, it stops the linking process and emits an
error message. The following example illustrates this situation. In this case, we are
trying to link the main.o file without providing another object file that contains a
definition of the exit label. Notice that the linker emits the error message undefined
reference to ‘exit’.

$ riscv64-unknown-elf-ld -m elf32lriscv main.o -o main.x


riscv64-unknown-elf-ld: warning: cannot find entry symbol start; ...
riscv64-unknown-elf-ld: main.o: in function ‘start’:
(.text+0x8): undefined reference to ‘exit’

3.2.4 Global vs local symbols


Symbols are classified as local or global symbols. Local symbols are only visible on the
same file, i.e., the linker does not use them to resolve undefined references on other
files. Global symbols, on the other hand, are used by the linker to resolve undefined
reference on other files.
By default, the assembler registers labels as local symbols. The .globl directive
is an assembly directive that instructs the assembler to register a label as a global
symbol. The following example shows an assembly program in which the .globl
directive is used to instruct the assembler to register the exit label as a global symbol
on the symbol table.

i # Contents of the exit.s file


2 .globl exit
з exit:
4 lia0, 0
5 li a7, 93
6 ecall

Assuming the code that invokes the exit function is located on the main.s file and
the exit function is located on the exit.s file, the following sequence of commands
shows how to assemble both files and link them together.

$ riscv64-unknown-elf-as -march=rv32im main.s -o main.o


$ riscv64-unknown-elf-as -march=rv32im exit.s -o exit.o
$ riscv64-unknown-elf-ld -m elf32lriscv main.o exit.o -o main.x
riscv64-unknown-elf-ld: warning: cannot find entry symbol start; ...

Notice that the linker is not producing the undefined reference to ‘exit’
anymore. It is worth noting that, in case the exit label is not registered as a global
label, the linker will not use it to resolve the undefined symbol and the linking process
will fail.

http://riscv-programming.org (Version: May 9, 2022) 31


Other documents randomly have
different content
Sheep,” which were found to be so, and therefore the Devil must
needs have done it. But in this he neither nameth the place, time, nor
Author to avouch it, and therefore all reasonable Men may judge how
palpable a falsity it is, for then if true it would follow that none could
be safe, and that the Devil might kill immediately with swords or
knives, which he cannot do.
2. Whereas he De Lithias. c. 8. p. Observ. 2.
holdeth that the 75.
Devil doth bring or convey the things to be Hist.
injected near unto the place, and that he
offereth filth and poysons to his clients, that thereby he may
fermentally conjoin the Ideas of these formed in the imaginative
faculty with these. If the Devil be taken to be meerly and simply
incorporeal, then he cannot remove matter (as we have before
proved) and so cannot convey the things near to the object; and if he
be taken to be corporeal (as we have asserted) his help is needless,
because the Witches may do it themselves, as we find sufficient
stories of their hideing of strange and poysonous things under the
thresholds of houses and Churches; and to this purpose this same
Author telleth us this story: “A certain person (he saith) did by
custome use to make water in a corner of the Court, whereupon he
was afflicted with a bloody and cruel Strangury. And all the remedy
of the Physicians proved in vain, except that as often as he did drink
of Birch-Ale he did find a signal ease: But as oft as he rose and
walked, and made water in the same place, so often his pains did
return. At the last a pin of old black Oak-wood is espied to be fixed in
the place where he used to make water. Which being pulled forth and
burned he remained free from the bloody Strangury, by drinking Ale
of Birchen-twiggs. Also (he saith) that he remembred, that
Karichterus had written that he had loosed such kind of
inchantments by only pissing through Beesomes of Birch.” Now from
hence it is plain that this making water constantly upon this pin of
old black Oak-wood did cause his bloody Strangury, and that the
pulling of it up and burning of it, was with the help of the Birchen Ale
the cure; but it can no wayes be judged necessary that the Devil
should fix the Oak pin there, but that the Witch might do it himself.
Neither can it be thought to be any power given by the Devil to the
Oaken pin, that it had not by nature, for in probability it will
constantly by a natural power produce the same effect; only thus far
the Devil had a hand in the action, to draw some wicked person to fix
the pin there where the Man was accustomed to make water, thereby
to hurt and torture him, and so was only evil in respect of the end.
3. We observe and affirm that whatsoever Observ. 3.
effects are brought to pass by that which is
commonly called and accounted Witchcraft, if they be not brought to
pass by jugling, confederacy, delusion and imposture (as the most of
them are, if not all) then they are performed either by meer natural
causes, or the strength of the Witches fancy, and most vehement
desire of doing of mischief to those she hateth, or by both joined
together, and that Satan is no further an author or actor, but as he
leadeth and draweth the minds of the Witches to do such
mischievous actions, and pusheth on to seek about to learn of others
such secret poysons, charms, images and other hidden things, that
being used so or so, may produce such destructive ends as their
wicked and diabolical purposes are led to, and in this sense they are
his clients, and bounden vassals, and not otherwise.
4. The stories that he relateth are either Observ. 4.
all to be taken to be true, or none of them;
and if they be all alike equally to be credited, then it will undeniably
follow, that they were all alike produced by natural causes, and so no
need at all of the Devils assistance in performing of them, no more
than by working upon the minds of such as used those natural means
to a wicked and mischievous end. For first he giveth these instances
of things that were very strange that were voided either by vomit or
stool, by the ordinary power of nature, without suspicion of
diabolical cooperation, as the voiding of the piece of the brass
Cannon with its letters, with the Eele wrapped in its secundines: The
Dragon that the Oxe voided by taking three herbs, with a tail like an
Eele, a body like or of leather, with a Serpentine head, and not less
than a Partridge: The knife that the Thieves forced a man to swallow,
which he voided by an Apostume in the side, and was after sound:
also the arrow head of three fingers broad strucken into the back,
and after voided by stool, with diverse such which we recited before.
And that these being solid bodies should have penetrated and passed
through parts that are impatient of wounds, and in which a wound is
mortal, must of necessity be very wonderful, and might as soon and
upon as rational grounds be taken to be diabolical, as those that he
enumerateth to be so: For from these it is manifest that either nature
put to her last pinch doth make penetration of dimensions, or else so
inlarge the pores, that those solid bodies may pass without wound,
which (if seriously considered) is a stupendious operation and effect.
And as there needeth no cooperation of a diabolical power, for the
performing of these, no more needeth there any concurrence of
Devils to the others, that to that purpose he relateth. Only here is all
the difference: these are wrought by the ultimate endeavour of the
Archæus to save life; without the concurrence of external causes; the
others (that are therefore called diabolical) are commonly wrought
for a bad end, namely to hurt or to take away life, and have an
external cause, to wit, the force of the Witches imagination and
strong desire of doing of mischief, which is stirred up to that end by
Satan, and therefore in regard of the end are devilish, though they be
both wrought by the agency of nature, the one in the body of the
imaginant, the other in the body that the Witch intendeth to hurt by
the force of her imagination and vehement desire, whereby a seminal
Idea is created or formed, which is sufficiently operative to
accomplish the end intended.
5. The Syl. Syl. Cent. 10. p. Observ. 5.
arguments that he 556.
bringeth to prove penetration of dimensions to be in nature, or
something equivalent thereunto, seem to be strong and convincing.
For in the generation of things, whosoever shall seriously and strictly
mark, shall find (as he alledgeth) that the spirit of the Archeus
(though not altogether incorporeal) doth in the seeds of things
penetrate it self, and their parts one another, which he further
maketh good by the instance of Gold generated of water; for it must
of necessity be, that more than fifteen parts of water must fall in or
penetrate one another, that from thence one part of Gold may be
made, for weight is not of nothing, but argueth the matter ponderous
in the Ballance. Therefore naturally the water must so oft penetrate
its body as the Gold doth preponderate the water. And though it be
granted that the water hath pores, yet notwithstanding it cannot
contain so much as fourteen times, it whole. And therefore he
irrefragably concludeth: Est ergo ordinarium in natura, quod
aliquæ partes aquæ se penetrent in unicum locum. And this he
backs with an unanswerable story of a Woman that longing for
Muscles, did in greediness eat some of them with the shells twice or
thrice broken with her teeth, and that she brought forth a child with
the same half eaten shells, and a wound in the belly; therefore those
shells had penetrated the stomach, womb and secundines, or
otherwise the force of the Archeus had opened the pores and letten
them pass in an unconceiveable manner. So that if these things be
granted to be true (and we confess we know not how they can be
answered) then there need no diabolical power be brought to solve
the injecting of strange things into mens bodies, seeing nature is
sufficient of it self, and therefore we can allow no power at all unto
Devils in effecting these things (if they be truly done, and be not
delusions) but only in drawing the minds of the Witches to these
wicked and mischievous courses; and therefore the Lord Bacon said
profoundly and wisely these words: Ut in operationibus illis earumq;
causis error cavendus est, ita quoq; danda vel imprimis opera est,
ne effecta nobis imponant, temere judicantibus talia esse, quæ
eousq; nondum processerunt. Sic prudentes judices, præscripta
velut norma, fidem haberi temere nolunt confessionibus sagarum,
nec etiam factorum contra illas probationi. Sagas enim turbat
imaginationis vertigo, ut putent se illud facere, quod non faciunt,
populumq; hîc ludit credulitas, ut naturæ opera imputent fascino.
6. And to confirm this point he addeth far Observ. 6.
more stupendious matters of fact than the
former, of things that were within, being taken to without or invisibly
conveyed away, as the woman at Mechlin that saw the Souldier in a
conflict lose his hand, and forthwith brought forth a Daughter
wanting an hand, which was never found, and the wench died of the
Hæmorrhage. Another at Antwerpe seeing a Souldier begging with
his right arm shot off and bloody, forthwith brought forth a Daughter
wanting the right arm whose bloody shoulder the Chirurgeon cured,
and she was married after; and that the arm was never found, neither
did there appear any bones or putrefied matter into which the arm
might waste. Also another Woman going to see the Decollation of
thirteen men; did soon after bring forth a mature Child with a bloody
neck, the head no where appearing. I confess it would rack the
judgment even of the most credulous to the highest pitch to believe
these unparallel’d Stories; but the Author relating them as of his own
knowledge, and being a person of unquestionable veracity, I cannot
conceive how they can rationally be denied, especially finding Mr
Boyle to affirm, that in those experiments (much more relations of
matters of fact) that Helmont avouched upon his own knowledge, he
durst be his Compurgator. Who would not believe but that these
things could never have been done, but by a supernatural and
Diabolical power, but that this Author (to which all judicious persons
in reason may adhere) doth utterly deny, that the arm was either
pull’d away or conveyed none can tell whither, by Satan, and
therefore that in such a strange Paradox, trivial reasons are not to be
allowed; and it were too much sloathfulness to ascribe all effects unto
Satan, of which we are ignorant. And therefore if an hand, an arm,
nay an whole head, could be separated from the rest of the body, and
conveyed forth of the Womb by the Archeus or natural spirit,
thereunto excited by the impression of horror and terror in the
Women: In like manner by the same power of the natural spirit of
man or woman, excited by a vehement and fierce imagination to
revenge and to do mischief, may strange things be injected (if there
can be any sound proof of such a matter of fact) into the bodies of
such men or women as the Witches intend to do hurt unto, and yet
Satan hath no more hand in it, but only as a spiritual agent to move
the wills of those wicked and malicious people to do mischief unto
those that they hate, though without cause. And the great secret of
that which may be called Witching, is the learning of others, who
likewise have had it by tradition, the great force of imagination, and
the natural spirit with the ways and means how to excite it and exalt
it; herein stands the mystery of all Magick, and it becomes only evil
in the use and application, and they are to be condemned that use it
to such devillish ends, even as those that use those good Creatures
that nature doth produce to poysonous, wicked, and destructive
purposes. And lastly, here we may note, that if things or bodies that
are without may be injected into the bodies of others, by the force of
exalted, imagination and a vehement desire, then the same power
that doth inject them through skin, flesh and bones, must also be
able to bring them near to the place, and need not at all the
assistance of Satan, because it is far easier to carry them near the
place, than to thrust them into the body; and so this Author hath
here introduced the Devils aid to bring them to the place to no
purpose, and never yet proved either by reason or matter of fact, that
ever Satan did any such thing, and so is a meer supposition without
proof.
7. The other De occult. nat. Observ. 7.
matters of fact that mirac. l. 2. c. 40. p.
he relateth are 325.
prodigious, and Hist. De Tumor. l. 6. c.
are brought to prove that Satan is an actor 19. p. 158.
to convey these strange things into the bodies of men, and are these.
A piece of an Oxe Hide taken forth of a mans Arm, so also that
Equuleum, a Wood-Horse, or a four-footed board with a wheel and
ropes twice as broad as the gullet. Another that vomited up perhaps
two thousand pins conglomerated together, with filth and hairs;
another that vomited up, he being present, wooden Chips that had
been cut off with the Hatchet in smoothing of wood, with much slime
to the bigness of two fists, of which we shall note these Conclusions.
1. It doth no way appear (if these things be granted to be true, both
for matter and manner) neither doth he offer to prove it, that these
are any more than the former Diabolical, but only in the end, because
they are for the hurt and destruction of mankind and not otherwise;
and there being no proof of the Devils Cooperation any further but in
working upon the minds of those that are agents and instruments to
bring these things to pass, we may very well reject those things that
are supposed, but not proved. 2. The ejecting or voiding of such
strange things as here he hath related, doth not necessarily suppose
their injection or thrusting in, because they may be bred there by
natural Causes, so Worms of many sorts and strange Figures, also
Frogs, Dracunculos and Askers have been voided, and doubtlesly
bred there by natural causes, and were not injected or thrust in, and
for proof of this I refer the Reader to the relations of learned
Schenchius lib. 3. p. 363. of those strange sorts of Worms and other
Creatures that he from divers Authors sheweth have been vomited
up, which without all scruple, were not injected, but bred there. To
confirm this and to prove what strange things are sometimes bred in
Apostumes and Tumors, we shall translate a passage or two, and first
take this from Levinus Lemnius that learned and famous Physician
of Zeland, who writeth thus: “Also forth of sordid Ulcers and
Impostures (he saith) we have known that the fragments of nails,
hairs, shells, little bones and stones have been taken forth; which
were concreted and grown together forth of putrid humours: As also
little creatures, worms with tails, and little beasts of an
unaccustomed form, cast up by vomiting, especially in those who
were oppressed with contagious diseases, in whose urines I have
often discerned to swim little Animalcles like to Pismires, or to those
creatures we observe in the estival months to move in the celestial
dew here in England we call it Woodsoar, or Cuckow-spittle.” Take
another from that learned and expert Chirurgeon Ambrosius Paræus
where he is speaking of strange tumors, in these words: “Also in
these tumors being opened thou maist see bodies of all kinds, and far
differing from the common matter of Tumors, as stones, chalk, sand,
coals, cockles, ears of corn, hay, horn, hairs, flesh as well hard as
spongious, grisles, bones and whole Animalcles, as well living as
dead. The generation of which things (by the corruption and
alteration of the humors) will not much astonish us, if we consider,
that even as nature hath framed Man as a Microcosm forth of all the
seeds and elements of the whole great world, that he might be as it
were the lively image of that great world: So in that Microcosm,
nature hath willed, that all the species of all motions and actions
might be manifest, nature being never idle in us, as long as matter is
not a wanting to work upon.” So that it is most plain that these
strange things may be bred within, and so the opinion of injecting
them, is but a meer figment. 3. Neither can the vomiting up of such
strange things as he relateth, conclude necessarily that they were
injected either by the power of Satan or the Witch, because they may
be performed by jugling, sleight of hand, confederacy and the like, as
was manifest in the Boy of Bilson, and diverse that we have known,
that had made some numbers of others to believe that they had
voided strange things, as pins, needles, crooked-knitting-pricks,
moss, nails, and the like; but upon a strickt search, have but proved
delusions and sleight, such as our common Hocus Pocus Men use,
when they make the people believe they swallow a long pudding of
white tinn, and again pull it forth of their mouths, or in pulling
ribbins, or laces of diverse colours forth of their throats. 4. And again
the most of these relations are but commonly taken upon trust from
the affirmations of the by-standers who might be confederate parties,
or ignorant persons, and so easily deceived; and it appeareth not that
Helmont was by at the very instant when the children vomited up the
wooden horse, or four-footed board, but that it was the by-standers
that drew it forth, who might be parties to the cheat, or be
themselves deluded, and so aver it pertinaciously to others. For I
have in my practice known a young Wench about 9 or 10 years old,
who that she might be pittied and have an idle life, had made her
Father and Mother believe that quick worms came forth at her ear,
and also I taking her into mine own house she had perswaded all the
family that it was true, and did often open her head-cloaths, and
holding down her ear a quick worm would drop forth of the hair, who
notwithstanding by diligent watching, was found out to get them
privately from under stones or wood, and so did cunningly convey
them into her hair, but being discovered, was by due correction
reclaimed, and so the wonder ceased. And it is as common to mistake
things, either by absolute judging them to be such a thing indeed,
when it hath but some slender resemblance of it, or by judging a
thing to be really so, because of such a name but metaphorically
given unto it; so it is usual to call a Carcinoma in the highest degree
Lupus or a Wolf, because as a Wolf is a most voracious creature, so
this ulcer is the most devouring of all others; and therefore have we
known after that such have been by incision eradicated by our selves
and others, and exposed to the view of the vulgar people, they would
presently most earnestly affirm to others that they had seen it, and
that it was a living creature, and had mouth, eyes and ears; so far will
ignorant mistake induce credulity.
8. That the force Syl. Syl. Cent. 10. p. Observ. 8.
of imagination 583.
accompanied with the passions of horror, fear, envy, malice, earnest,
desire of revenge, and the like, is great upon the body imaginant, as
also upon the fœtus in the womb, is acknowledged by all. But that it
can at distance work upon another body, though denied by Fienus
and the whole rabble of the Schoolmen, yet is strongly proved by this
learned Author, and allowed of by all others that truly understood
the operations of nature, which we also take to be a certain truth, and
do assert that if those people that are esteemed Witches, do really
and truly (of which we utterly doubt) inject any of these strange
things into the bodies of men, that they are brought to pass meerly
by the imagination of the Witch, and the Devil acteth nothing in it at
all, but the setting of his will upon that mischief. As for the handling
the dispute concerning the manner of the injecting of these strange
things, so strongly pursued by this Author, Sennertus and others, we
shall totally supersede and suspend our judgment, until the ὅτι be
sufficiently proved (which yet lies under water, and unseen) and then
it will be time enough to dispute the manner, when the matter is
certainly made evident. Therefore we will shut up this with that
modest and grave advice of the Lord Bacon in these words: Ideo
cogemur in hac inquisitione ad nova experimenta confugere; ubi
directiones tantùm eorum præscribi possunt, non ulla positiva in
medium adferri. Si quis putet subsistendum nobis fuisse, donec
tentamentis res penitus innotuisset, (ut fecisse nos ubiq; probant
alii tituli) sciat dubia nos fide amplecti quæcunq; imaginationis
effecta circumferuntur, animum tamen esse illa per otium exigere
ad Lydium veritatis lapidem, id est, experimentorum lucem.
CHAP. XIII.

That the ignorance of the power of Art and Nature and such like
things, hath much advanced these foolish and impious opinions.

The opinions that we reject as foolish and impious are those we have
often named before, to wit, that those that are vulgarly accounted
Witches, make a visible and corporeal contract with the Devil, that he
sucks upon their bodies, that he hath carnal copulation with them,
that they are transubstantiated into Cats, Dogs, Squirrels, and the
like, or that they raise tempests, and fly in the air. Other powers we
grant unto them, to operate and effect whatsoever the force of
natural imagination joyned with envy, malice and vehement desire of
revenge, can perform or perpetrate, or whatsoever hurt may be done
by secret poysons and such like wayes that work by meer natural
means.
And here we are to shew the chief causes that do and have
advanced these opinions, and this principally we ascribe to mens
ignorance of the power of Nature and Art, as we shall manifest in
these following particulars.
1. There is nothing more certain than, that how great soever the
knowledge of Men be taken to be, yet the ultimate Sphere of natures
activity or ability is not perfectly known, which is made most
manifest in this, that every day there are made new discoveries of her
secrets, which prove plainly that her store is not yet totally
exhausted, nor her utmost efficiency known. And therefore those
Men must needs be precipicious, and build upon a sandy foundation,
that will ascribe corporeal effects unto Devils, and yet know not the
extent of nature, for no Man can rationally assign a beginning for
supernatural agents and actions, that does not certainly know where
the power and operation of nature ends.
2. And as it is thus in general, so in many particulars, as especially
in being ignorant of many natural agents that do work at a great
distance, and very occultly, both to help, and to hurt, as in the
weapon salve, the Sympathetick powder, the curing of diseases by
mumial applications, by Amulets, Appensions and Transplantions,
which all have been, and commonly are ascribed unto Satan, when
they are truly wrought by natural operations. And so (as we have
sufficiently manifested before) by many strange, and secret poysons
both natural and artificial, that have no bewitching power in them at
all, but work naturally, and only may be hurtful in their use through
the devilishness of some persons that use them to diverse evil ends.
3. There is nothing that doth more clearly manifest our scanted
knowledge in the secret operations of nature, and the effects that she
produceth, than the late discoveries of the workings of nature, both
in the vegetable, animal and mineral Kingdoms, brought dayly to
light by the pains and labours of industrious persons: As is most
evident in those many elucubrations, and continued discoveries of
those learned and indefatigable persons that are of the Royal Society,
which do plainly evince that hitherto we have been ignorant of
almost all the true causes of things, and therefore through blindness
have usually attributed those things to the operation of Cacodemons
that were truely wrought by nature, and thereby not smally
augmented and advanced this gross and absurd opinion of the power
of Witches.
4. Another great means in advancing De occult. Philos. l.
these Tenents hath been Mens supine 1. c. 2.
negligence in not searching into and experimenting the power of
natural agents, but resting satisfied in the sleepy notions of general
rules, and speculative Philosophy. By which means a prejudice hath
been raised against the most occult operations of nature, and natural
magick (which is (as Agrippa truly said) “The comprizer of great
power, full of most high mysteries, and containeth the most
profound contemplation, nature, power, quality, substance and
virtue of most secret things, and the knowledge of all nature) to be
condemned, as the work of the Devil and hellish fiends, which is the
handmaid and instrument of the Almighty.” And from this diabolical
pit of the ignorance of the power of nature (especially when assisted
by art) have sprung up those black and horrid lies in the mouths of
Erastus, Conringius and above all of Kircherus, denying the
possibility of the transmutation of metals, by the power of Art and
Nature, and ascribing the performance thereof by Paracelsus,
Lullius, Sendinogius and others to the Devil; so malevolent do men
grow when they are led by nescience and ignorance.
5. The ignorance of the strange and Vid. Theatr. Chym.
wonderful things that Art can bring to pass Vol. 5. p. 943.
hath been no less a cause, why the most admirable things that Art
bringeth to pass by it are through blind ignorance ascribed unto
Devils, for so have many brave learned Artists, and Mechanicians
been accused for Conjurers, as happened to Roger Bacon, Dr. Dee,
Trithemius, Cornelius Agrippa, and many others, when what they
performed was by lawful and laudable art. The strange things that
the Mathematicks and Mechanicks can perform are hardly to be
enumerated, of which were those most wonderful catoptrical glasses
mentioned by Nicero, Aquilonius, Baptista Porta and many others,
those wonderful engines in the shape of Birds, Men, Beasts, and
Fishes that do move, sing, hiss and many such like things mentioned
by Heron of Alexandria, and our Countryman Dr. Fludd; and those
that would have more ample satisfaction concerning the stupendious
things that are produced by art, may receive most large satisfaction
in reading that most learned and elaborate Epistle written as a
preface before the Book of Johannes Ernestus Burgravius called
Biolychnium vel de lampade vitæ et mortis, by Marcellus
Vranckheim Doctor of both laws, as also in reading that profound
and mysterious piece written by Roger Bacon, de admirabili
potestate artis et naturæ, et de nullitate magiæ, with the learned
notes of Dr. Dee upon it, of which he saith this: Ut videatur quod
omnis potestas magica sit inferior his operibus et indigna. And
therefore there can be nothing more unworthy, than for any man,
that pretendeth to any portion of reason, so far to dote, or suffer
himself to be led with ignorance and rashness, as to ascribe those
strange things that Nature and Art, or both joined together do
produce, unto Devils: And yet there is nothing that is more common
not only by the blind vulgar, but even by those that otherwise would
be accounted learned, and wise enough; pride and folly attendeth the
most of the Sons of Men.
6. Another gross mistake there is, in Hist.
supposing those strange things that are
performed by vaulters, tumblers, dancers upon ropes, and such like,
not possible to be done but by the assistance of the Devil, when they
are altogether brought to pass and effected by use, custome, exercise,
nimbleness and agility of body. And yet we have known some not
only of the popular rank, but many that thought themselves both
wise, learned and religious that have been so blind as to father these
things upon Devils and seriously to seem to believe, that the actors of
these things had made a league and compact with the Devil, by
whose help they performed them. And I do remember that a pretty
active young man, within these few years went about in this North
Countrey with a neat Bay Mare for money to shew tricks, which were
very odd and strange, for if she had been blindfolded, and several
pieces of money taken from several persons, and wrapped in a
cloath, the Mare would have given every one their own piece of
money; and this and many other feats she plaid, were not only by the
common people, but by others that should have been more wise,
judged to be performed by no other means but by the Devil, and
some were so stark mad as to believe and affirm that the Mare was
not a natural one, but that it was the Devil that plaid those strange
tricks in the shape of a Mare: when more sober judgments knew that
they were performed by the masters eye, and rod directing the Mare.
Error & credulitas multum in hominibus possunt.
7. In like manner are often both those that are learned, as well as
the vulgar most wofully imposed upon by the odd and strange feats
performed by Legierdemain, sleight of hand, and by wonderful
things brought to pass by subtile and cunning Impostors that act by
confederacy, and the like, of which we have given some instances
before in this treatise. And it was no evil piece of service, that Master
Scot did in his book of the discovery of Witchcraft, when he laid open
all the several tricks of Legierdemain and sleight of hand, thereby to
undeceive the ignorant multitude; and that is no less praise-worthy
that is performed by the Author of that little treatise called Hocus
Pocus junior, where all the feats are set forth in their proper colours,
so that the most ignorant may see how they are done, and that they
are miracles unknown, and but bables being discovered, which
treatise I could commend to be read of all Witchmongers and vain
credulous persons, that thereby their ignorance may be laid open,
and they convinced of their errors.
8. The ignorance or mistaking of these things, joyned with the
notions Men have imbibed from their infancy, together with
irreligious education, are the true and proper causes, that make so
many ascribe that power to Devils and Witches, that they neither
have, or ever had, or can ever bring into act. And therefore it
behoveth all that would judge aright of these abstruse matters, to
labour to understand the secret operations of nature, and the strange
works of art, to divest themselves of their false imbibed notions, and
truely and rightly to understand the Articles of the Christian Faith, to
be daily conversant in reading the Scriptures, they will then be more
fit to judge of these things, and not to call light darkness, nor
darkness light.
CHAP. XIV.

Of diverse Impostures framed and invented to prove false and


lying miracles by, and to accuse persons of Witchcraft, from
late and undeniable authorities.

In the treatise preceeding we have often made mention of delusions


and Impostures, which we shall largely handle in this place: and
though Mr. Glanvil, and others do object, that though many
pretended possessions or Witchcrafts have been proved to be meer
couzenings and impostures, yet therefore it will not follow that all are
so. To which we shall render these answers.
1. If it do not necessarily conclude, that they are all impostures, yet
it gives a most shrewd cause of dubitation that they may be so. And
the objection depends not upon a necessary connexion betwixt the
subject and predicate, for some being direct and palpable
Impostures, it is not of necessity, but by contingency or accident that
the others are not so, and ought first to have been proved, which
never yet was performed.
2. But we affirm that a general conclusion drawn from an inductive
argument is good and sound, where no instance can be clearly made
out to the contrary. But as yet no true instance, really and faithfully
attested, hath ever been brought to prove that any of these things
that we deny, were ever effected by diabolical power. For who were
ever by and present, that were persons of sincerity and sound
judgment, that could truly testifie and averr that the Devil in a visible
and corporeal shape made a contract with the Witch, or that he suckt
upon his, or her body, or that he had carnal copulation with them, or
that saw when the Witch was really changed into a Dog or a Cat, or
that they flew or were carried in the air? Seeing no instance can be
given to prove any of these to be undoubted truths, it must needs
follow that they are meer figments, or at the best all but absolute
Impostures. And again it is but precarious, and petitio principii, to
imagine that any persons have vomited up or voided strange things
that saw or knew that they were injected by Devils, for they were
either naturally bred there, or else were meer Impostures and
delusive Juglings.
And therefore we shall propose some Histories of strange and
prodigious cheats and Impostures from late and unquestionable
authorities, whereby all the rest may be judged and discerned; of
which take this for one.
“1. Elizabeth Vid. Stat. Pulton, Hist. 1.
Barton of Kent (by 25. year Henr. 8. c.
those that 12.
laboured to cry up The Pope. Vid Chron.
her horrible cheats for miracles, otherwise Hollingshead. Stow
An. Hen. 8. 25. p.
called the holy Maid of Kent) and others 1013.
were in the twenty fifth year of King Henry
the Eighth attainted of High Treason, for that under colour of
hypocrisie, Revelations and false Miracles practised by the said
Elizabeth, they conspired to impugne and slander the divorce
between the King and Queen Katherine his first Wife, and the last
Marriage between him and Queen Anne his second Wife, to destroy
the King, and to deprive him of his Crown.” Her false and feigned
miracles, and the subtile and cunning contrivances that were brought
to pass by the help of her confederate accomplices, and her and the
others open confession of them may be found at large in
Hollingshead, Stow, and the writings of Mr. Lambert, whither for
brevities sake I remit my reader, and shall only give it here in the
words of Speed, which are these: “The Romanists (he saith) much
fearing that Babel would down, if Queen Anne might be heard
against wicked Haman, sought to underprop the foundations thereof
with certain devices of their own: and that the same might pass
without note of suspicion, they laid their forgery even upon Heaven it
self; whose pretended oracle Elizabeth Barton (commonly called the
holy Maid of Kent) was made to be; and the pillars of this godless
Fabrick were Edward Bocking a Monk by profession, and Doctor of
Divinity, Richard Masters Parson of Aldington, the Town wherein
she dwelt; Richard Deering a Monk, Hugh Rich a Friar, John
Adestone and Thomas Abell Priests, put to their helping hands; and
Henry Gould Batchelor of Divinity, with John Fisher the reverend
Father of Rochester imployed their pains to dawb these downfalling
walls with their untempered morter. The Scribes that set their pens
for her miracles, were Edward Thwaites Gentleman, and Thomas
Lawrence Register, besides Haukherst a Monk, who writ a letter that
was forged to be sent her from Heaven; And Richard Risby and
Thomas Gould were the men that dispersed her miracles abroad to
the world. This holy Maid Elizabeth made a Votaress in Canterbury,
was taught by Bocking her Ghostly Father, and suspected Paramour,
to counterfeit many feigned trances, and in the same to utter many
virtuous words for the rebuke of sin, under which more freely she
was heard against Luthers doctrine, and the Scriptures translation,
then desired of many: neither so only, but that she gave forth from
God and his Saints by sundry suggestive Revelations, that if the King
proceeded in his Divorce, and second Marriage, he should not raign
in his Realm one month after, nor rest in Gods favour the space of an
hour. But the truth discovered by Gods true Ministers, this oracle
gave place as all other such did, when Christ by his death stopped
their lying mouths: For her self and seven of her disciples were
executed for Treason at Tiburn, and the other six put to their fines
and imprisonment.” To which he subjoineth this story of the like
nature. “With the like counterfeit Revelations and feigned
predictions this generation of hypocrites had brought Edward Lord
Stafford Duke of Buckingham, unto his unhappy end, by the working
of John de la Court his own Confessor, together with Nicholas
Hopkins a Monk of the Carthusian Order in the Priory of Henton in
Somersetshire, who by his visions from Heaven forsooth, heartned
him for the Crown; But before his own Coronet could aspire to that
top, he worthily lost both head and all upon Tower-hill for his
Treason, Anno Domini 1521. Unto such sins the world was then
subject, and into such conceits their reputed holiness had brought
them, not only among the simple and unlettered, but even with them
that seemed to be learned indeed: For by certain predictions
foreshewing a great deluge, Prior Bolton of S. Bartholomews in
London, was so fearful that he built himself a house upon the height
of Harrowhill, storing it with provisions necessary to keep himself
from drowning in Anno Dom. 1524.
2. And that we Stow’s Chron. p. Hist. 2.
may be certified 678.
how frequent and common these counterfeited Impostures have
been, and yet are practised, take this other from undoubted
authority. “The 15 of August being Sunday in the 16 of the raign of
Queen Elizabeth, Agnes Bridges a Maid about the age of 20 years,
and Rachel Pinder a Wench about the age of 11 or 12 years, who both
of them had counterfeited to be possessed by the Devil (whereby they
had not only marvellously deluded many people both Men and
Women, but also diverse such persons, as otherwise seemed of good
wit and understanding) stood before the Preacher at Pauls-cross;
where they acknowledged their hypocritical counterfeiting with
penitent behaviours, requiring forgiveness of God and the world, and
the people to pray for them. Also their several examinations and
Confessions were there openly read by the Preacher, and afterwards
published in print, for posterity hereafter to beware of the like
deceivers.” From whence we may take these two Observations.
1. We may from hence note, how subject Observ. 1.
the nature of man is both to deceive and to
be deceived, and that not only the common people, but also the wiser
and more learned heads may most easily be imposed upon. And, that
therefore in things of this nature, and the like, we cannot use too
much circumspection, nor use too much diligence to discover them.
2. We may note, that when such strange Observ. 2.
Impostures or false Miracles are pretended,
there is commonly some sinister and corrupt end aimed at, under the
colour of Religion, and that those that are most ready to publish such
things as true Miracles and Divine Revelations, are generally those
that did complot and devise them. And therefore the greater number
they be that cry them up, and the more esteem the persons are of
that blow abroad such things, the greater suspicion we ought to have
of the falsity and forgery of them. Always remembring that the
greater the fame and number of the persons are that conspire and
confederate together, the greater things they may bring to pass, and
be more able to deceive, as was manifest by the Priests attending the
Oracles; who, though they laboured to father their predictions upon
some Deity, yet it was manifest that it was nothing else, but their
own Confederacy, Impostures and Juglings.
3. But these Vid. A Book called, Hist. 3.
Diabolical A discovery of
Counterfeitings of fraudulent
practices
possessions, and concerning
the maintaining of pretended
the power of possessions.
dispossession and Vid. ibid. Dialog. 11.
casting forth of Devils, was not only upheld p. 352.
and maintained by the Papists to advance
their superstitious courses; but also in the said time of Queen
Elizabeth, there were divers Non-Conformists, to gain credit and
repute to their way, that did by publick writing labour to prove the
continuation of real possessions by Devils, and that they had power
by fasting and Prayer to cast them out. Of which number were one
Mr Darrell and his Accomplices, who not only writ divers Pamphlets
in the positive defence of that opinion, but also published certain
Narrations of several persons, that they pretended were really
possessed with Devils, which were cast forth by their means in using
Fasting and Prayer. Which writings were answered by Mr Harsnet
and others, and their Theory not only overthrown, but their practice
discovered to be counterfeiting and Imposture. Whereupon there
were divers persons suborned to feign and counterfeit possessions,
as William Sommers of Nottingham, who by the Exorcists was
reported to have strange fits, passions and actions; which are at large
described and set forth in that learned Treatise, Dialogical
Discourses of Spirits and Devils, written about the same time by
John Deacon and John Waller, Ministers, and of divers other
persons who likewise pretended the same counterfeit possessions.
And though the said forged and feigned possessions were strongly
maintained by their Abettors, and the matters of fact audaciously
asserted to be true; yet after the said Darrell and his Accomplices
were examined by the Queens Commissioners, all was made
apparent to be notorious counterfeiting, cheating and imposture,
both by the confession of Sommers himself, and by the Oaths of
several Deponents. Neither was that discourse containing the certain
possession of seven persons in one Family in Lancashire, at
Cheworth in the Parish of Leigh, in the Year 1594. (though believed
by many for a truth, because of the streight tale told by the said
Darrell in that Narrative) of any better grain, but full of untruths,
impossibilities, absurdities and contradictions.
4. Our next Vid. The cunning of Hist. 4.
instance shall be a the Boy of Bilson, p.
most strange 55.
imposture acted in the time of King James, and in a manner known
unto the whole Nation; that is of the Boy of Bilson in Staffordshire,
in the year 1620. by name William Perry, whose condition as he had
been taught, and so left by the Popish Priests, take as followeth. “This
Boy being about thirteen years old (but for wit and subtilty far
exceeding his age) was thought by divers to be possessed of the Devil,
and bewitched, by reason of many strange fits and much distemper,
wherewith he seemed to have been extreamly affected. In those fits
he appeared both deaf and blind, writhing his mouth aside,
continually groaning and panting, and (although often pinched with
mens fingers, pricked with Needles, tickled on his sides, and once
whipped with a Rod, besides other the like extremities) yet could he
not be discerned by either shrieking or shrinking to bewray the least
passion or feeling. Out of his fits he took (as might be thought) no
sustenance which he could digest, but together with it, did void and
cast out of his mouth, rags, thred, straw, crooked pins, &c. Both in
and out of his fits his belly (by wilful and continual abstinence
defrauding his own Guts) was almost as flat as his back, besides, his
throat was swoln and hard, his tongue stiff and rolled up towards the
roof of his mouth, insomuch that he seemed always dumb, save that
he would speak once in a Fortnight or three Weeks, and that but in
very few words.
“Two things there were which gave most just cause of presumption
that he was possessed and bewitched; one was that he could still
discern when that Woman (which was supposed to have bewitched
him) to wit Jone Cocke was brought in to any room where he was,
although she were secretly conveyed thither, as was one time tryed
before the Grand Jury at Stafford: The second, that though he would
abide other passages of Scripture, yet he could not indure the
repeating of that Text, viz. In the beginning was the word, &c. Jo. 1.
ver. 1. but instantly rolling his eyes and shaking his head, as one
distracted, he would fall into his usual fits of groaning, panting,
distraction, &c. In which plight he continued many months, to the
great wonder and astonishment of thousands, who from divers parts
came to see him.” Thus much of his cunning.
Yet notwithstanding, this most devillish and cunningly contrived
counterfeiting and dissimulation was discovered and fully detected
by the sagacity of that pious and learned person, Dr Thomas Morton
then Bishop of Coventry and Lichfield: To whose memory I cannot
but owe and make manifest all due respect, because he was well
known unto me, and by the imposition of whose hands I was
ordained Presbyter when he was Bishop of Durham, and also knew
his then Secretary, Mr Richard Baddeley, who was the Notary, and
writ the examination of this crafty Boy. The manner how such a
doubtful and intricate piece of Imposture was found out and
discovered, you may read at large in the Treatise called a Discourse
concerning Popish Exorcising. And his publick Confession we shall
give in the Authors own words: “He was finally brought again to the
Summer Assizes held at Stafford, the 26. of July, Anno 1621. where
before Sir Peter Warburton and Sir Humfrey Winch Knights, his
Majesties Justices of Assize, and the face of the County and Country
there assembled, the Boy craved pardon first of Almighty God, then
desired the Woman there also present to forgive him; and lastly,
requested the whole Country whom he had so notoriously and
wickedly scandalized, to admit of that his so hearty Confession for
their satisfaction.
“And thus it pleased God (he saith) to open the eyes of this Boy
(that I may so say) luto with the Clay of the Romish Priests lewd
Impostures, and sputo with the spittle of his own infamy, to see his
errors and to glorifie the God of truth. And though many such
Impostures as this have in several ages been hudled up in darkness
and recorded for true stories, by those that were Partisans to them
and Confederates with them, yet doubtless were but of the same
stamp with this, and might all as well have been discovered, if the
like care, skill and industry had been used.
5. No less Vid. The Hist. 5.
villanous, bloody arraignment and
and Diabolical, tryal of Witches at
Lancaster, 1612.
was the design of
Thompson alias Southworth, Priest or Jesuit, against Jennet Bierley,
Jane Southworth, and Ellen Bierly of Samesbury in the County of
Lancaster, in the year 1612. the sum of which is this. “The said
Jennet Bierley, Ellen Bierley, and Jane Southworth, were Indicted at
the Assizes holden at Lancaster upon Wednesday the nineteenth of
August, in the year abovesaid, for that they and every of them had
practised, exercised, and used divers devillish and wicked Arts, called
Witchcrafts, Inchantments, Charms and Sorceries, in and upon one
Grace Sowerbutts. And the chief witness to prove this was Grace
Sowerbutts her self, who said that they did draw her by the hair of
the head, and take her sense and memory from her, did throw her
upon the Hen-roost and Hay-mow; did appear to her sometimes in
their own likeness, sometimes like a black Dog with two feet, that
they carried her where they met black things like men that danced
with them, and did abuse their bodies; and that they brought her to
one Thomas Walsham’s House in the night, and there they killed his
Child by putting a nail into the Navil, and after took it forth of the
Grave, and did boil it, and eat some of it, and made Oyl of the bones,
and such like horrid lies.” But there appearing sufficient grounds of
suspicion that it was practised knavery, the said Grace Sowerbutts
was by the wisdom, and care of Sir Edward Bromley Knight, one of
his Majesties Justices of Assize at Lancaster, appointed to be
examined by William Leigh and Edward Chisnal Esquires, two of his
Majesties Justices of peace in the same County, and so thereupon
made this free confession. Being demanded “whether the accusation
she laid upon her Grandmother, Jennet Bierley, Ellen Bierley and
Jane Southworth, of Witchcraft, viz. of the killing of the child of
Thomas Walshman, with a nail in the Navil, the boyling, eating and
oyling, thereby to transform themselves into divers shapes, was true?
She doth utterly deny the same, or that ever she saw any such
practises done by them. She further saith, that one Mr. Thompson,
which she taketh to be Mr. Christopher Southworth, to whom she
was sent to say her prayers, did perswade, counsel and advise her, to
deal as formerly hath been said against her said Grandmother, Aunt
and Southworths Wife.
“And further she confesseth, and saith, that she never did know, or
saw any Devils, nor any other visions, as formerly hath been alledged
and informed.
“Also she confesseth, and saith, that she was not thrown, or cast
upon the Hen-roust, and Hay-mow in the Barn, but that she went up
upon the Mow by the wall side. Being further demanded whether she
ever was at the Church, she saith, she was not, but promised
hereafter to go to Church, and that very willingly; of which the
author of the relation gives this judgment.
“How well (he saith) this project, to take away the lives of three
innocent poor creatures by practice and villany, to induce a young
Scholar to commit perjury, to accuse her own Grandmother, Aunt,
&c. agrees either with the title of a Jesuit, or the duty of a religious
Priest who should rather profess sincerity and innocency, than
practise treachery! But this was lawful, for they are Hereticks
accursed, to leave the company of Priests, to frequent Churches, hear
the word of God preached, and profess religion sincerely.”
6. But we shall shut up the relating of Hist. 6.
these prodigious and hellish stories, of these
kind of couzening and cheating delusions and impostures, with one
instance more that is no less notorious than these that we have
rehearsed. About the year 1634 (for having lost our notes of the
same, we cannot be so exact as we should) there was a great
pretended meeting of many supposed Witches at a new house or
barn, in Pendle Forest in Lancashire, then not inhabited, where (as
the accusation pretended) some of them by pulling by a rope of Straw
or Hay, did bring Milk, Butter, Cheese, and the like, and were carried
away upon Dogs, Cats or Squirrels. The informer was one Edmund
Robinson (yet living at the writing hereof, and commonly known by
the name of Ned of Roughs) whose Father was by trade a Waller, and
but a poor Man, and they finding that they were believed and had
incouragement by the adjoyning Magistrates, and the persons being
committed to prison or bound over to the next Assizes, the boy, his
Father and some others besides did make a practice to go from
Church to Church that the Boy might reveal and discover Witches,
pretending that there was a great number at the pretended meeting,
whose faces he could know; and by that means they got a good living,
that in a short space the Father bought a Cow or two, when he had
none before. And it came to pass that this said Boy was brought into
the Church of Kildwick a large parish Church, where I (being then
Curate there) was preaching in the afternoon, and was set upon a
stall (he being but about ten or eleven years old) to look about him,
which moved some little disturbance in the Congregation for a while.
And after prayers I inquiring what the matter was, the people told
me that it was the Boy that discovered Witches, upon which I went to
the house where he was to stay all night, where I found him, and two
very unlikely persons that did conduct him, and manage the
business; I desired to have some discourse with the Boy in private,
but that they utterly refused; then in the presence of a great many
people, I took the Boy near me, and said: Good Boy tell me truly, and
in earnest, did thou see and hear such strange things of the meeting
of Witches; as is reported by many that thou dost relate, or did not
some person teach thee to say such things of thy self? But the two
men not giving the Boy leave to answer, did pluck him from me, and
said he had been examined by two able Justices of the Peace, and
they did never ask him such a question, to whom I replied, the
persons accused had therefore the more wrong. But the Assizes
following at Lancaster there were seventeen found guilty by the Jury,
yet by the prudent discretion of the Judge, who was not satisfied with
the evidence, they were reprieved, and his Majesty and his Council
being informed by the Judge of the matter, the Bishop of Chester was
appointed to examine them, and to certifie what he thought of them,
which he did; and thereupon four of them; to wit Margaret Johnson,
Francis Dicconson, Mary Spenser, and Hargrieves Wife, were sent
for up to London, and were viewed and examined by his Majesties
Physicians and Chirurgeons, and after by his Majesty and the
Council, and no cause of guilt appearing but great presumptions of
the boys being suborned to accuse them falsely. Therefore it was
resolved to separate the Boy from his Father, they having both
followed the women up to London, they were both taken and put into
several prisons asunder. Whereupon shortly after the Boy confessed
that he was taught and suborned to devise, and feign those things
against them, and had persevered in that wickedness by the counsel
of his Father, and some others, whom envy, revenge and hope of gain
had prompted on to that devillish design and villany; and he also
confessed, that upon that day when he said that they met at the
aforesaid house or barn, he was that very day a mile off, getting
Plums in his Neighbours Orchard. And that this is a most certain
truth, there are many persons yet living, of sufficient reputation and
integrity, that can avouch and testifie the same; and besides, what I
write is the most of it true, upon my own knowledge, and the whole I
have had from his own mouth more than once.
Thus having brought these unquestionable Histories to manifest
the horrid cheats and impostures that are practised for base, wicked
and devillish ends, we must conclude in opposing that objection
proposed in the beginning of this Chapter, which is this: That though
some be discovered to be counterfeitings and impostures, yet all are
not so, to which we further answer.
1. That all those things that are now Reas. 1.
adayes supposed to be done by Demoniacks
or those that pretend possessions, as also all those strange feats
pretended to be brought to pass by Witches or Witchcraft, are all
either performed by meer natural causes (for it is granted upon all
sides that Devils in corporeal matter can perform nothing but by
applying fit actives to agreeable passives.) And miracles being long
since ceased, it must needs follow, that Devils do nothing but only
draw the minds of Men and Women unto sin and wickedness, and
thereby they become deceivers, cheats and notorious impostours: so
that we may rationally conclude that all other strange feats and
delusions, must of necessity be no better, or of any other kind, than
these we have recited, except they can shew that they are brought to
pass by natural means. Must not all persons that are of sound
understanding judge and believe that all those strange tricks related
by Mr. Glanvil of his Drummer at Mr. Mompessons house, whom he
calls the Demon of Tedworth, were abominable cheats and
impostures (as I am informed from persons of good quality they were
discovered to be) for I am sure Mr. Glanvil can shew no agents in
nature, that the Demon applying them to fit patients, could produce
any such effects by, and therefore we must conclude all such to be
impostures.
2. It is no sound way of reasoning, from Reas. 2.
the principles of knowing, either thereby to
prove the existence of things, or the modes of such existence, because
the principle of being is the cause of the principle of knowing, and
not on the contrary, and therefore our not discovering of all
Impostures that are or have been acted, doth not at all conclude the
rest that pass undiscovered, are diabolical or wrought by a
supernatural power; for it ought first to be demonstrated that there
are now in these days some things wrought by the power of Devils,
that are supernatural, in elementary and corporeal matter, which
never was nor can be, as from the testimonies of all the learned we
have shewed before. And therefore a man might as well argue that
there are no more thieves in a Nation, but those that are known, and
brought to condign punishment, when there may be, and doubtless
are many more; so likewise there are many hundreds of impostures,
that pass and are never discovered, but that will not at all rationally
conclude that those must be diabolical that are not made known.
CHAP. XV.

Of divers Creatures that have a real existence in Nature, and yet


by reason of their wonderous properties, or seldom being seen,
have been taken for Spirits, and Devils.

Before we come to speak of Apparitions in Hist. 1.


general, we shall premise some few things
by way of caution, because there is not one subject (that we know of)
in the World that is liable to so many mistakes, by reason of the
prepossessed fancies of men, in adhering to those fictions of Spirits,
Fairies, Hobgoblins, and many such like, which are continually
heightned by ignorant education, and vain melancholy fears. We
shall not mention those many apparitions that are frequently
practised by forgery and confederacy, for base ends and interests, as
have been commonly used in the time of Popery, and attempted in
our dayes, though with little success. As also by other persons for
base lucre or worse intents, of which we have known some notorious
ones that have been discovered. Neither shall we speak of those
feigned ones that have been practised to hide thievery and roguery,
as we once knew that certain persons who stole mens sheep in the
night, did carry them away upon a thing made like a Bier covered
with a white sheet, by which means those that saw them took it to be
an apparition, and so durst not come near them, and so the most
part of the people of 3 or 4 Villages were terrified, and the report was
far spred that it was a walking spirit, and yet at last discovered to be
a cunning piece of knavery to hide their theft withal. Neither shall we
say any thing of those ludicrous apparitions that are often practised
to terrifie, abuse, and affright others. But we shall here give the
relation of some strange creatures, that seldom being seen or found,
have induced more ignorant persons to take them for Demons, and
these we shall enumerate in this order.
1. It hath been, Centur. 1. Hist. 9. p. Hist. 2.
and still is a strong 18.
opinion amongst the vulgars and Witchmongers also, that Witches
transforming themselves into diverse shapes, did in the night time
enter into peoples houses, and then and there suck the breasts or
navils of infants in their Beds or Cradles, that thereby they were
weakned or consumed away; which inveterate opinion was the more
firmly believed, because children that at night were very well, in the
morning were found to be very ill, and to have been sucked in the
places aforesaid. To clear which point take this Observation from the
learned pen of Thomas Bartholinus that was Physician to Frederick
King of Denmark, in English thus. “Three infants (he saith) of the
Pastor Fionens at Lyckisholm, which is a noble Mannor belonging to
the very illustrious Lord Christian Thomæus Sehsted, the Kings
Chancellor, Eques Auratus, and a most renowned Senator of
Denmark, my Mecænas, that were sleeping in their accustomed
Chamber, were not long after troubled with an unwonted bewailing
and inquietude, that they felt themselves to be sucked or milked of
something. The nipples of their breasts being diligently handled by
the Parents did confirm the Childrens suspicion, because they did
hang out like a Womans that did give suck. And to prevent this
fascination, the nipples of the breasts were anointed with
preservatives against poyson and other bitter things. Hereupon their
Navils were so worn with vehement suction, that not only they were
prominent or did hang out, but also did as it were shew the greatness
of the mouth that had sucked by the impression remaining. But the
Infants being carried forth of the Chamber, did from thenceforth rest
free from any suction, especially being carried in peoples arms. And
this Caprimulgus or Goat-milker, is by Bellonius said to be in Crete
of the bigness of a Cuckow, being very hurtful to the Goats, insomuch
that it sucketh milk from their dugs on the nights.” By which we may
plainly understand, how Creatures that are but seldom seen, or
whose properties are unknown, may easily effect those things that
ignorant heads may impute unto Witchcraft.
2. It is no less Isai. 34. 14. De quadr. l. 1. p.
believed by many, Isai. 13. 21. 862.
that those kind of Deut. 32. 17. Levit. 17. 7.
Creatures which 2 Chron. 11. 15. Psal. 106. 37.
are called Satyres
are but a kind of Demons; for learned Gesner reckoning them to be a
kind of Apes, doth tell us this: “Even as (he saith) the Apes
Cynocephali, or with Dogs-heads, have given the occasion of the
Fable, that some have thought such to be men: So Satyrs being also a
rare kind of Apes, and of greater admiration, some have believed
them to be Devils: also of some men deluded by the Poets and
Painters, as also Statuaries, who have feigned that they had Goats
feet and horns, the more to augment the admiration and
superstition, they have been thought Devils: when in Ape-Satyres
there is no such thing to be seen.” And this opinion hath been the
more strengthened because the most of the Translators have in the
Old Testament rendered the word ‫( ָׂשִעיר‬which properly signifieth an
happy man or beast) a Goat, a Satyre, (as Gen. 27. ver. 11. Esau my
Brother is a hairy man; where the very same word is used) Demon,
or Devil. But it is plain that it did and doth signifie no more but only
Satyrs, as will appear by these reasons. 1. First, as our English
Translators have truly rendred it in that of Isaiah, And the Satyre
shall cry unto his fellow: for it is certainly related, both by ancient
and modern Navigators, that in those desolate Islands where there
are store of them, they will upon the nights make great shouting and
crying, and calling one unto another. And in another place of the
same Prophet it is said by the same Translators, and Satyres shall
dance there; dancing being one of the properties of that hairy
Creature, as a thing it is much delighted with, and so are but Satyres
that are natural Creatures and not Devils. 2. And though the same
Translators have rendred the plural of the same word, by the name
Devils, yet it there properly signifieth also Satyres; for though in
another place it be said; they sacrificed to devils, not to God, and so
again by the Psalmist, for they sacrificed their sons and daughters
unto devils; where in both places the word is ‫ ֵשִד ים‬vastatoribus, to
the destroyers or to Devils; because in those Idols the Devils were
worshipped, and thereby destroyed the souls of men: 3. Yet it is
manifest that their Idols were formed in the shape of Satyres, in a
most terrible manner; for the late and most credible travellers that
have been in those parts of Asia, where those Idolatries are still
upholden, do unanimously relate that they make their Images or
Idols that they worship, as terrible and frightful as they can devise, as
may be seen in the relations of the Travels of Vincent le Blanc,
Mandelslo, and Ferdinand Mendez Pinto, and Mr Herbert our
Countryman gives us the Idol of the Bannyans in the ugly shape of a
monstrous Satyre. 4. So that though this worshipping and sacrificing,
in respect of its abominableness, filthiness and Idolatrousness, was
yielded to Devils, which spiritually and invisibly ruled in these
Children of disobedience, and was the Author of all those delusions
and impostures; yet it doth no where appear, that it was Demons in
the corporeal shape of Satyres (as many have erroneously supposed)
no more than the golden Calves that Jeroboam made, were real
Devils: but these Idols were made in the figure or shape of Satyrs or
hairy Creatures, as saith the Text: And he ordained him Priests for
the high places, and for the hairy Idols or Satyres, and for the Calves
that he had made. It is the same Hebrew word here that our English
Translators render Devils, that in the two former places of Isaiah
they translate Satyres; and as the Calves are not rendred Devils, why
should the Images that were like Satyres be translated so? Surely the
Devil was as much in the Calves, and as much worshipped in those
dumb Idols as he was in the dumb and dead Idols or Images of the
Satyres, and so no more reason to call the one Devils than the other.
But that which totally overthrows the conceit that they should be real
Devils in corporeal shapes and figures, is this, that both the Calves
and the Images of these Satyres were made by Jeroboam: now it is
manifest that he could not make a real Devil, but only Images of
Calves and Satyres, wherein and whereby the Devils might be
worshipped in those Idolatrous ways.
So that it is most Observ. Medic. lib. Hist. 3.
apparent, that 3. c. 56. p. 283.
these Satyres being seldom seen and of strange qualities, have made
many to believe that they were Demons; nay it seems their Images
and Pictures have been taken for Devils, and yet are but meer natural
Creatures, and by learned men accounted a kind of Apes, which we
shall now prove by an undeniable instance or two; and first this from
the pen of that learned Physician Nicholaus Tulpius, who saith thus:
“In our remembrance (he saith) there was an Indian Satyre brought
from Angola; and presented as a gift to Frederick Henry Prince of
Aurange. This Satyre was four-footed and from the humane shape
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebooknice.com

You might also like