| MASM - Program development - BIN - Qbasic - binary conversion |
Binære konverteringer kan udføres i Qbasic, men det går meget hurtigere i maskinkode:
bin : konverterer fra decimalværdi
til binært tal (angivet som en tekst-streng)
- fx fra 157 til "10011101".
bins : konverterer fra binært tal
(angivet som en tekst-streng) til decimalværdi
- fx fra "10011101" til 157
| bin.asm |
;-----------------------------QBASIC
;CALL ABSOLUTE(b%, b$, bin)
; input : b% : a value - ex: 157
; output : b$ : a string with length = 16, filled with spaces
; for the computed binary string - ex: "100011101"
; code : bin : where this code is loaded with BLOAD
;-----------------------------MASM header
ASSUME CS:CODE
CODE SEGMENT
ORG 0 ;this is a binary file
;-----------------------------BSAVE/BLOAD header
head: db 0FDh ;BSAVE/BLOAD ID
dw 9999h ;segment - not used - any value is ok
dw 9999h ;offset - not used - any value is ok
dw Last-First ;computed size of code
;-----------------------------Code
First:
push bp ;standard entry
mov bp,sp ;[bp+0] : old bp
;[bp+2] : QBASIC return offset
;[bp+4] : QBASIC return segment
;[bp+6] : offset b$ descriptor
;[bp+8] : offset b%
mov bx, [bp+6] ;b$ descriptor
mov si, [bx+2] ;b$ offset
add si, 16 ;point after last char
mov bx, [bp+8] ;b% offset
mov ax, [bx] ;b%
do: dec si ;point to previous char
mov cx, ax ;b%
and cx, 1 ;test last bit of b%
jz zero
mov byte ptr [si], '1'
jmp next
zero: mov byte ptr [si], '0'
next: shr ax, 1 ;shift all bits in c% 1 pos right
jnz do ;repeat if c% is not zero
fin: pop bp ;standard exit
retf 4 ;far return - clean up two parameters
;-----------------------------
Last:
CODE ENDS
END head
|
| bins.asm |
;-----------------------------QBASIC: ;CALL ABSOLUTE(b$, b%, VARPTR(bins)) ; input : b$ : a binary string - ex: "10011101" ; output : b% : the returned value of bs$ - ex: 157 ; code : bins : a variable used as container for the code loaded by BLOAD ;-----------------------------MASM header: ASSUME CS:CODE CODE SEGMENT ORG 0 ;this is a binary file ;-----------------------------BSAVE/BLOAD header: head: db 0FDh ;BSAVE/BLOAD ID dw 9000h ;segment - not used - any value is ok dw 0000h ;offset - not used - any value is ok dw Stop-Start ;computed size of code ;-----------------------------Code: Start: push bp ;standard QBASIC entry mov bp,sp ;[bp+0] : old bp ;[bp+2] : QBASIC return segment ;[bp+4] : QBASIC return offset ;[bp+6] : offset b% (last parameter) ;[bp+8] : offset b$ descriptor mov bx,[bp+8] ;b$ descriptor mov cx,[bx] ;b$ length mov si,[bx+2] ;b$ offset add si,cx ;initialize bit pointer: last+1 mov ax,0 ;initial b% value mov bx,1 ;initial bit value inc cx
do: dec cx ;count bits
jz return ;stop when done
dec si ;next bit - from last to first
mov dl,[si] ;read bit
sub dl,48 ;convert from ascii to value (asc("0")=48)
jz loop ;if "0": skip
add ax,bx ;if "1": add bit value to b% (= ax)
loop: add bx,bx ;compute next bit value (* 2)
jmp short do
return: mov si,[bp+6] ;b% offset
mov [si],ax ;return final b% value
pop bp ;standard QBASIC exit retf 4 ;far return - clean up 2 parameters: ;bs$ and b% (2 bytes each) Stop: ;-----------------------------MASM end: CODE ENDS END head ;first code - placed at ORG 0 |