forked from recluze/assembly-lang-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
c08-02.asm
120 lines (93 loc) · 2.37 KB
/
c08-02.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
[org 0x0100]
jmp start
message: db 'hello world', 0
clrscr:
push es
push ax
push cx
push di
mov ax, 0xb800
mov es, ax
xor di, di
mov ax, 0x0720
mov cx, 2000
cld ; auto-increment mode
rep stosw ; rep cx times, store words
; source is ax for word, al for bytes
; destination is es:di
; inc/dec di as well by 2 bytes
pop di
pop cx
pop ax
pop es
ret
printnum:
push bp
mov bp, sp
push es
push ax
push bx
push cx
push dx
push di
; first, let's split digits and push them onto the stack
mov ax, [bp+4] ; number to print
mov bx, 10 ; division base 10
mov cx, 0 ; total digit counter
nextdigit:
mov dx, 0 ; zero out
div bx ; divides ax/bx .. quotient in ax, remainder in dl
add dl, 0x30 ; convert to ASCII
push dx ; push to stack for later printing
inc cx ; have another digit
cmp ax, 0 ; is there something in quotient?
jnz nextdigit
; now let's do the printing
mov ax, 0xb800
mov es, ax
mov di, 0
nextpos:
pop dx ; digit to output. Already in ASCII
mov dh, 0x07 ; why is this inside the loop here?
mov [es:di], dx
add di, 2
loop nextpos ; cx has already been set, use that
pop di
pop dx
pop cx
pop bx
pop ax
pop es
pop bp
ret 2
strlen:
push bp
mov bp,sp
push es
push cx
push di
les di, [bp+4] ; load DI from BP+4 and ES from BP+6
mov cx, 0xffff ; maximum possible length
xor al, al ; value to find
repne scasb ; repeat until scan does not become NE to AL
; decrement CX each time
mov ax, 0xffff
sub ax, cx ; find how many times CX was decremented
dec ax ; exclude null from the length
pop di
pop cx
pop es
pop bp
ret 4
start:
call clrscr
push ds
mov ax, message
push ax
call strlen ; return value is in AX
push ax
call printnum ; print out the length
mov ah, 0x1
int 0x21
mov ax, 0x4c00
int 0x21