assembler
Paspēlējos ar asembleri. Uzrakstīju pāris ērtas funkcijas ievadei, izvadei un teksta konvertēšanai skatļos un atpakaļ. Apbrīnojiet. Es gribēju teikt, varbūt kādam noder. :)
CODE SEGMENT
ASSUME CS:CODE, DS:CODE
ORG 100h
PROGRAM: jmp main
msg_welcome db "Si programma rekina skaitla kvadratu."
newline db 13, 10, 0
msg_input db "Ievadiet skaitli: ", 0
msg_result db "Skaitla kvadrats: ", 0
buffer db 256 dup(?)
strlen proc ;push offset string, pop length
push bp
mov bp, sp
push ax
push bx
push cx
push di
mov ax, 0
mov cx, -1
mov bx, [bp+4]
mov di, bx
repne scasb
sub di, bx
dec di
mov [bp+4], di
pop di
pop cx
pop bx
pop ax
pop bp
ret
endp
print proc ;push offset string
push bp
mov bp, sp
push ax
push bx
push dx
mov ah, 40h
mov bx, 1
mov dx, [bp+4]
push dx
call strlen
pop cx
int 21h
pop dx
pop bx
pop ax
pop bp
ret 2
endp
input proc ;push offset buffer (minimum 256 bytes)
push bp
mov bp, sp
push ax
push cx
push dx
push si
push di
mov ah, 0ah
mov di, [bp+4]
mov byte ptr [di], 254
mov dx, di
int 21h
mov ah, 02h
mov dl, 10
int 21h
mov si, di
inc si
inc si
xor cx, cx
mov cl, [di+1]
rep movsb
mov byte ptr [di], 0
pop di
pop si
pop dx
pop cx
pop ax
pop bp
ret 2
endp
tostr proc ;push number, push offset buffer (minimum 7 bytes)
push bp
mov bp, sp
push ax
push bx
push cx
push dx
push si
xor si, si
mov bx, [bp+4]
mov ax, [bp+6]
cmp ax, 0
jne fi0
mov byte ptr [bx], '0'
mov byte ptr [bx+1], 0
jmp ex2
fi0: jge fi1
mov byte ptr [bx], '-'
inc bx
neg ax
fi1: mov cx, 10000
lp1: cwd
div cx
cmp si, 0
jne if3
cmp ax, 0
jne if3
jmp fi3
if3: add al, 30h
mov [bx+si], al
inc si
fi3: push dx
mov ax, cx
cwd
mov cx, 10
div cx
mov cx, ax
pop ax
cmp cx, 0
jg lp1
mov byte ptr [bx+si], 0
ex2: pop si
pop dx
pop cx
pop bx
pop ax
pop bp
ret 4
endp
toint proc ;push offset string, pop number (result is undefined if it can't fit into a signed word)
push bp
mov bp, sp
push ax
push bx
push cx
push dx
push si
push di
mov bx, 1
xor cx, cx
mov si, [bp+4]
push si
call strlen
pop di
cmp di, 0
jg fi4
mov word ptr [bp+4], 0
jmp ex1
fi4: add di, si
lp2: dec di
mov al, [di]
cmp al, 30h
jl br1
cmp al, 39h
jg br1
sub al, 30h
cbw
mul bx
cmp bx, 10000
je fi5
add cx, ax
mov ax, bx
mov bx, 10
mul bx
mov bx, ax
cmp si, di
jne lp2
jmp fi5
br1: cmp al, '-'
jne fi5
neg cx
fi5: mov [bp+4], cx
ex1: pop di
pop si
pop dx
pop cx
pop bx
pop ax
pop bp
ret
endp
main:
push offset msg_welcome
call print
push offset msg_input
call print
push offset buffer
call input
push offset buffer
call toint
pop ax
imul ax
push ax
push offset buffer
call tostr
push offset msg_result
call print
push offset buffer
call print
push offset newline
call print
ret
CODE ENDS
END PROGRAM