13
1 CSE 2312 Lecture 12 Assembly Language Programming Computer Organization & Assembly Language Programming

Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture12.pdf–The assembler uses mnemonics, that is, short words such as ADD,

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture12.pdf–The assembler uses mnemonics, that is, short words such as ADD,

1

CSE 2312

Lecture 12 Assembly Language Programming

Computer Organization &

Assembly Language Programming

Page 2: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture12.pdf–The assembler uses mnemonics, that is, short words such as ADD,

2

Assembly vs. Machine Language

• Machine Language– Every computer has an ISA (Instruction Set Architecture). It is a set

of registers, instructions, and other features visible to its low-levelprogrammers. It is commonly referred to as machine language,although not entirely accurate.

– A program at this level is a long list of binary numbers, one perinstruction, telling which instructions to execute and what theiroperands are.

• Assembly Language– Programming with binary numbers is very difficult to do, so all

machines have an assembly language, a symbolic representation ofthe instruction set architecture, with symbolic names like, ADD,SUB, and MUL, instead of binary numbers.

– We introduce assembly language programming for one specificmachine, the Intel 8088, which was used in the original IBM PC andwas the base from which the modern Pentium grew.

– We introduce how to use some tools that can be downloaded to helplearn about assembly language programming.

– This lecture will NOT try to train experienced assembly languageprogrammers, but to help beginner learn about computerarchitecture through hands-on experience.

Page 3: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture12.pdf–The assembler uses mnemonics, that is, short words such as ADD,

3

Assembly Language

• Mnemonics– The assembler uses mnemonics, that is, short words such as ADD,

SUB, and MUL for machine instructions such as add, subtract, andmultiply, to make them easy to remember.

– The assemblers allow the use of symbolic names for constants andlabels to indicate instruction and memory addresses.

– The assemblers support some number of pseudoinstructions, which donot translate into ISA instructions, but which are commands to theassembler to guide the assembly process.

• Assembler– When a program in assembly language is fed to a program called an

assembler, the assembler converts the program into a binary programsuitable for actual execution. This program can then be run on theactual hardware.

– However, beginners often make errors and the binary program juststops, without any clue as to what went wrong.

– To make life easier for beginners, we run the binary program not on theactual hardware, but on a simulator, which executes one instruction at atime and gives a detailed display of what it is doing. Debugging is theneasier.

– Programs running on a simulator run very slowly, but this is fine forlearning assembly language program, rather than run a production job

Page 4: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture12.pdf–The assembler uses mnemonics, that is, short words such as ADD,

4

Tracer

• Tools– Debugging in assembly language programming is hard and

troublesome without tools for beginners.

– Beginners often make different kinds of errors and mistakes.

– Tools is helpful for beginners to debug their assembly languageprograms

• Assembler– Our programming is based on a toolkit that includes a simulator,

called the interpreter or tracer, as it interprets and traces theexecution of the binary program step by step as it runs.

– The terms “simulator,” “interpreter,” and “tracer” will be usedinterchangeably.

– Usually, when we are talking about just executing a program, we willspeak of the “interpreter” and when we are talking about using it asa debugging tool, we will call it the “tracer,” but it is the sameprogram.

Page 5: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture12.pdf–The assembler uses mnemonics, that is, short words such as ADD,

5

A Small Assembly Language Program

(a) An assembly language program. (b) The corresponding tracer display.

Page 6: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture12.pdf–The assembler uses mnemonics, that is, short words such as ADD,

6

Program and Tracer Screen

• Program– The above Figure shows an assembly language program for the

8088

– The numbers following the exclamation marks are the source linenumbers, to make it easier to refer to parts of the program.

– A copy of this program can be found in the accompanying material,in the directory examples in the source file HlloWrld.s.

– The program has the suffix .s, which indicates that it is an assemblylanguage source program.

• Tracer Screen– The tracer screen contains seven windows, each containing

different information about the state of the binary program beingexecuted.

Page 7: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture12.pdf–The assembler uses mnemonics, that is, short words such as ADD,

7

Windows in Tracer Screen

• Windows in Tracer Screen– The top left window shows the contents of the processor, consisting of

the current values of the segment registers, CS, DS, SS, and ES, thearithmetic registers, AH, AL, AX, and others.

– The middle window in the top row contains the stack, an area ofmemory used for temporary values.

– The right-hand window in the top row contains a fragment of theassembly language program, with the arrow showing which instructionis currently being executed. As the program runs, the current instructionchanges and the arrow moves to point to it.

– The strength of the tracer is that by hitting the return key (labeled Enteron PC keyboards), one instruction is executed and all the windows areupdated, making it possible to run the program in slow motion.

– Below the left window is a window that contains the subroutine callstack, here empty. Below it are commands to the tracer itself. To theright of these two windows is a window for input, output, and errormessages.

– Below these windows is a window that shows a portion of memory.– The tracer shows the source program, the machine registers, and quite

a bit of information about the state of the program being executed.– As each instruction is executed the information is updated, allowing the

user to see in great detail what the program is doing.

Page 8: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture12.pdf–The assembler uses mnemonics, that is, short words such as ADD,

8

Registers

• Registers in the 8088 Processor– Every processor, including the 8088, has an internal state, where it

keeps certain crucial information.

– Thus, the processor has a set of registers where this informationcan be stored and processed.

– Probably the most important of these is the PC (program counter),which contains the memory location, that is, the address, of the nextinstruction to be executed.

– This register is also called IP (Instruction Pointer). This instruction islocated in a part of the main memory, called the code segment.

• Different Registers– General registers

– Segment registers

– Pointer and index registers

– Condition code register or Flag register

– Instruction pointers (program counter)

Page 9: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture12.pdf–The assembler uses mnemonics, that is, short words such as ADD,

9

General Registers

Page 10: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture12.pdf–The assembler uses mnemonics, that is, short words such as ADD,

10

Hello World Program

(a) HlloWrld.s (b) The corresponding tracer windows

Page 11: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture12.pdf–The assembler uses mnemonics, that is, short words such as ADD,

11

Assembly Programing: Step by Step

• Write your program– HlloWrld.s

– Editor

• Compile or Translate the source code– ./as88 HlloWrld.s

• Debugging the code after compiled successfully with Trace– ./t88 HlloWrld

• Running the code directly– ./s88 HlloWrld

Page 12: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture12.pdf–The assembler uses mnemonics, that is, short words such as ADD,

12

“Hello” Program

_EXIT = 1 ! 1

_WRITE = 4 ! 2

_STDOUT = 1 ! 3

.SECT .TEXT ! 4

start: ! 5

MOV CX,de-hw ! 6

PUSH CX ! 7

PUSH hw ! 8

PUSH _STDOUT ! 9

PUSH _WRITE ! 10

SYS ! 11

ADD SP,8 ! 12

SUB CX,AX ! 13

PUSH CX ! 14

PUSH _EXIT ! 15

SYS ! 16

.SECT .DATA ! 17

hw: ! 18

.ASCII "Hello Yeqing Li\n" ! 19

de: .BYTE 0 ! 20

.SECT .BSS

Constants Definition

Count the length of string

Parameter 3 of WRITE: length of string

Parameter 2 of WRITE: address of string

Parameter 1 of WRITE: output of device

System Call ID

System Call

Clean Stack

Check if all characters are out

Return value

EXIT the system call ID

System CALL

Define the name for the string

Add the tail null character

The string

Page 13: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture12.pdf–The assembler uses mnemonics, that is, short words such as ADD,

13

“Hello” Program

_EXIT = 1 ! 1

_WRITE = 4 ! 2

_STDOUT = 1 ! 3

.SECT .TEXT ! 4

start: ! 5

MOV CX,de-hw ! 6

PUSH CX ! 7

PUSH hw ! 8

PUSH _STDOUT ! 9

PUSH _WRITE ! 10

SYS ! 11

ADD SP,8 ! 12

MOV CX,ded-sn ! 13

PUSH CX ! 14

PUSH sn ! 15

PUSH _STDOUT ! 16

PUSH _WRITE ! 17

SYS ! 18

ADD SP,8 ! 19

SUB CX,AX ! 20

PUSH CX ! 21

PUSH _EXIT ! 22

SYS ! 23

.SECT .DATA ! 24

hw: ! 25

.ASCII "Hello UTA\n" ! 26

de: .BYTE 0 ! 27

sn: ! 25

.ASCII "Hello CSE2312\n" ! 26

ded: .BYTE 0 ! 27

.SECT .BSS