Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Carter P.A.PC assembly language.2005.pdf
Скачиваний:
15
Добавлен:
23.08.2013
Размер:
1.07 Mб
Скачать

2.1. WORKING WITH INTEGERS

35

 

2.1.4

Example program

 

 

 

 

math.asm

 

1

%include "asm_io.inc"

 

 

 

 

 

2

segment .data

; Output strings

3

prompt

db

"Enter a number: ", 0

4

square_msg

db

"Square of input is ", 0

5

cube_msg

db

"Cube of input is ", 0

6

cube25_msg

db

"Cube of input times 25 is ", 0

7

quot_msg

db

"Quotient of cube/100 is ", 0

8

rem_msg

db

"Remainder of cube/100 is ", 0

9

neg_msg

db

"The negation of the remainder is ", 0

10

 

 

 

 

 

11

segment .bss

 

 

 

 

12

input resd 1

 

 

 

13

14segment .text

15global _asm_main

16_asm_main:

17

enter

0,0

; setup routine

18

pusha

 

 

19

 

 

 

20

21

22

mov

eax, prompt

call

print_string

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

call

read_int

 

mov

[input], eax

 

imul

eax

; edx:eax = eax * eax

mov

ebx, eax

; save answer in ebx

mov

eax, square_msg

 

call

print_string

 

mov

eax, ebx

 

call

print_int

 

call

print_nl

 

mov

ebx, eax

 

imul

ebx, [input]

; ebx *= [input]

mov

eax, cube_msg

 

call

print_string

 

mov

eax, ebx

 

call

print_int

 

call

print_nl

 

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

36

CHAPTER 2. BASIC ASSEMBLY LANGUAGE

imul

ecx, ebx, 25

; ecx = ebx*25

mov

eax, cube25_msg

 

call

print_string

 

mov

eax, ecx

 

call

print_int

 

call

print_nl

 

mov

eax, ebx

 

cdq

 

; initialize edx by sign extension

mov

ecx, 100

; can’t divide by immediate value

idiv

ecx

; edx:eax / ecx

mov

ecx, eax

; save quotient into ecx

mov

eax, quot_msg

 

call

print_string

 

mov

eax, ecx

 

call

print_int

 

call

print_nl

 

mov

eax, rem_msg

 

call

print_string

 

mov

eax, edx

 

call

print_int

 

call

print_nl

 

neg

edx

; negate the remainder

mov

eax, neg_msg

 

call

print_string

 

mov

eax, edx

 

call

print_int

 

call

print_nl

 

popa

 

 

mov

eax, 0

; return back to C

leave

 

 

ret

math.asm

 

2.1.5Extended precision arithmetic

Assembly language also provides instructions that allow one to perform addition and subtraction of numbers larger than double words. These instructions use the carry flag. As stated above, both the ADD and SUB instructions modify the carry flag if a carry or borrow are generated, respectively.

Соседние файлы в предмете Электротехника