;sprintf for dos via fasmg, i need help to compile: mov ax,[bp+dx]
;(https://board.flatassembler.net/topic.php?p=190878)
;
include '8086.inc'
include 'format/mz.inc'
macro stdcall proc,args&
iterate arg, args
indx 1+%%-%
mov bx,arg
push bx
end iterate
call proc
end macro
entry code:start ; program entry point
stack 100h ; stack size
segment code
start:
push cs
pop ds
call main
mov ah,4ch
int 21h
; sprintf(soutput, sformat, [args])
; sformat: string containing the pattern of the output string
;%s: string
;%d: decimal number
;%h: hexadecimal number in lower case
;%h: hexadecimal number in upper case
;
;%d, %h and %h also accepts ":[number of fixed digits]"
;the number in the square brackets should then be in[0..9] and equal one less the number of fixed digits...
; soutput: pointer to buffer which will receive the output string
; [args]: ...
;
; alpha_addition db 0
; digit_count db 0
; put these two variables in the data section:
max_dx equ 8
sprintf:
;enter 0,0
push bp
mov bp,sp
mov dx,max_dx
mov di,[bp+4]
mov si,[bp+6]
push dx
mov dx,si
;call _tips
pop dx
.loop:
lodsb
cmp al,"%"
je .format
stosb
test al,al
jnz .loop
.done:
xor ax,ax
jmp .finish
.error:
or ax,-1
.finish:
;leave
mov sp, bp
pop bp
pop ax
sub dx,max_dx/2;8
add sp,dx
jmp ax
.format:
lodsb
cmp al,"%"
jne .not_percent
stosb
jmp .loop
.not_percent:
mov [digit_count],0
push ax
cmp byte [si],":"
jne .not_extra
lodsb
cmp byte [si],":"
je .not_extra
lodsb
cmp al,"0"
jl .error
cmp al,"9"
jg .error
sub al,2fh
mov [digit_count],al
.not_extra:
pop ax
cmp al,"s"
je .string
cmp al,"h"
je .number.lhex
cmp al,"h"
je .number.uhex
cmp al,"d"
jne .error
.number.dec:
mov bx,10
call .number
jmp .loop
.number.lhex:
mov byte [alpha_addition],27h
mov bx,16
call .number
jmp .loop
.number.uhex:
mov byte [alpha_addition],7
mov bx,16
call .number
jmp .loop
.number:
push bx
mov bx,bp
add bx,dx
mov ax,[bp+dx];[bp+dx]
pop bx
add dx,max_dx/4;4
push dx
xor cx,cx
.number.loop:
xor dx,dx
div bx
push dx
inc cx
test ax,ax
jnz .number.loop
cmp [digit_count],0
je .number.putchar
push cx
mov ax,cx
mov cl,byte [digit_count];;;
sub cx,ax
jz .number.prefix.end
js .number.prefix.end
mov al,"0"
.number.prefix:
stosb
loop .number.prefix
.number.prefix.end:
pop cx
.number.putchar:
pop ax
add al,30h
cmp al,39h
jle .number.putchar.nonalpha
add al,[alpha_addition]
.number.putchar.nonalpha:
stosb
loop .number.putchar
pop dx
ret
.string:
push si
push bx
mov bx,bp
add bx,dx
mov si,[bp+dx];[bp+dx]
pop bx
add dx,max_dx/4;4
.string.loop:
lodsb
test al,al
jz .string.end
stosb
jmp .string.loop
.string.end:
pop si
jmp .loop
main:
push bp
mov bp, sp
;
stdcall sprintf,soutput,sformat,hi,16,start2,start
add sp,12
;
mov dx,soutput
mov ah,9
int 21h
;
mov dx,2
add dx,30h
mov ah,2
int 21h
mov sp, bp
pop bp
ret
;segment data
alpha_addition db 0
digit_count db 0
sformat db "%s! %d-lol",13,10
db "%s = offset: 0x%h:7$"
hi db "hi$"
start2 db "start$"
soutput rb 100h