Download 1.2 Assembly and Machine Instructions

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1.2 Assembly and Machine Instructions
A program is actually a list of numbers. A computer can only stores numbers in its memory. Since we use a
logic “1” or “0” in the circuitry all numbers are in fact in binary. The numbers are represented in
hexadecimal values so that we reduce the number of digits needed in the representation.
A compiler converts your program into machine code. One can program directly in machine code using an
assembler. An assembler converts a program written assembly code (almost machine code) into machine
code. The assembly code is built using machine instruction but symbols are used instead of numbers to
facilitate programming.
The following is the Hello World program written in assembly language:
title Hello World Program
; This program displays "Hello, world!"
dosseg
.model small
.stack 100h
.code
main proc
mov ax,@data
mov ds,ax
mov
mov
int
ah,09h
dx,offset msg
21h
ax,4C00h
21h
main
mov
int
endp
.data
msg
end
db
main
'Hello World!',0Ah,0Dh,'$'
(hello.asm)
The assembler converts the assembly code to machine code by looking up codes. The
following is the assembled machine code:
Turbo Assembler
Version 2.0
hello.ASM
Hello World Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
02/02/98 13:03:54
Page 1
(hello.asm)
; This program displays "Hello, world!"
0000
0000
0000
0000
0000
0003
B8 0000s
8E D8
0005
0007
000A
dosseg
.model
.stack
.code
main
small
100h
proc
mov
mov
ax,@data
ds,ax
B4 09
BA 0000r
CD 21
mov
mov
int
ah,09h
dx,offset msg
21h
000C
000F
0011
B8 4C00
CD 21
mov
int
endp
ax,4C00h
21h
0011
0000
.data
48 65 6C 6C 6F 20 57+
6F 72 6C 64 21 0A 0D+
24
end
msg
db 'Hello World!',0Ah,0Dh,'$'
main
main
Notice the ASCII codes.
The actual program is the machine codes in the left column. The code is
stores aeparatly from the data. The following is the machine code for
the program:
B8 00 00 8E D8 B4 09 BA 00 00 CD 21 B8 4C 00 CD 21
And the data is:
48 65 6C 5C 6F 20 57 6F 72 6C 64 21 0A 0D 24
The memory dump of the area in memory that the program resides is:
You can dig out the machine code and data.
A compiler converts high-level source code like C into machine code. It skips the
assembly code step.
The machine code is unique to each platform. An executable program is a program that is
already compiled and is in machine code. An executable program compiled on a PC can
not run on a Sun or Mac for example.
Related documents