Code: Select all
mov ax,730h
mov edi,0B8000h
mov [edi],ax ; will write "0" in top left corner
Code: Select all
mov ax,730h
mov edi,0B8000h
mov [edi],ax ; will write "0" in top left corner
Because your ASM example doesn't do the same thing (printing a parameterized character to a parameterized location).rdos wrote:Why are such simple things so hard in C?
That's easy to fix:Solar wrote:Because your ASM example doesn't do the same thing (printing a parameterized character to a parameterized location).
Code: Select all
; AH = attribute
; AL = char
; BL = row
; BH = col
WriteChar Proc
push edi
push ax
mov al,80
mul bl
add al,bh
adc ah,0
add ax,ax
mov edi,0B8000h
or di,ax
pop ax
mov [edi],ax
pop edi
ret
WriteChar Endp
Yes, but it is unreadable.Solar wrote:Moreover, your ASM source is specific to a certain assembler, and won't necessarily compile on a different one (AT&T vs. Intel syntax). The C source discussed here would compile on any C compiler.
Code: Select all
; AL Char
; BL Fore color
; BH Back color
; CX Column
; DX Row
WriteChar Proc
push ds
push ax
push di
mov ah,bh
shl ah,4
or ah,bl
push ax
push dx
mov ax,80
mul dx
add ax,cx
add ax,ax
mov di,ax
mov ax,dosb800
mov ds,ax
pop dx
pop ax
mov [di],ax
pop di
pop ax
pop ds
ret
WriteChar Endp
Well, it consists of a set of unreadable macros instead. There is nothing more frustrating than to try to figure out how a particular #define is evaluated by the compiler when these are generated based on a zillion other defines.berkus wrote:It's unreadable because it was written in a pretty lame way. See Solar's improvement, which is heaps more readable.rdos wrote:Yes, but it is unreadable.
Neither "unreadable" nor "a set" (since there is only one macro), hence I call "troll".rdos wrote:Well, it consists of a set of unreadable macros instead.berkus wrote:See Solar's improvement, which is heaps more readable.rdos wrote:Yes, but it is unreadable.
This version suffers from the same problem as the original - it fails to recognise that often you're printing a string and you only need to do the "address = start + (y * 80 + x) * 2" calculation once for the first character, then "address += 2" for each additional character. Basically, the next step in optimisation is to realise "putch()" is virtually irrelevant because you're using "putstring()" for almost everything instead.Solar wrote:I took the liberty of optimizing your code further: