Page 1 of 1

Asm help

Posted: Sat Dec 21, 2002 8:22 am
by Silenger
Sorry if i wrote stupid code :)
I am just learning asm and i want to write a 'a' at a line i choose in PMode

pop ax ;Pop line
shl ax, 41 ;Line * 80 * 40
xor di, di ;Zero
mov di, ax ;Set line
mov ax, 0B8000h ;Screen mem !To big
mov si, ax ;Set screen
mov ah, 'a' ;Put a in ax
mov si:[di], ah ;Write char in mem !wrong
inc di ;Next byte
mov ah, 0x07 ;Set attr
mov si:[di], ah ;Write attr in mem !wrong

I can't find what i am doing wrong....

If you know some realy good tut plz post them thx

Re:Asm help

Posted: Sat Dec 21, 2002 9:32 am
by drizzt
If you are in PMode try with this code... I hope I've been more clear as possible:

Code: Select all

; These are memory locations:
x_pos   dw   0
y_pos   dw   0

; Remember video_location = (y_pos*80 + x_pos) + 0xB8000
print_A_on_the_screen:

mov   ax, [y_pos]   ; ax = y_pos
mov   cx, 80
mul   cx      ; ax = y_pos*80
add   ax, [x_pos]   ; ax = y_pos*80 + x
shl   ax, 1      ; ax = ax * 2 (every location is 1 word = 2 bytes)

mov   bl, 'A'
mov   bh, 0x07    ; <attribute/color>

mov   [eax+0xB8000], bx   ; Write a grey A on the screen
Otherwise you can read this tutorial on Bona Fide OS site:
http://osdev.neopages.net/tutorials/basickernel.php

It explain how to write simple printf function in PMode without BIOS ints (asm & C)

Re:Asm help

Posted: Sat Dec 21, 2002 10:40 am
by Silenger
I wrote this based on yours:
I used bootf02 boot loader from John Fine
But it do nothing it just restarts the comp

You used eax but the value is in ax?
I don't get it, is the value in ax also in eax?

mov [eax+0xB8000], bx ???

Code: Select all

ORG 0xFF800000

BITS 32

SEGMENT .data

SEGMENT .text
START:

PUSH 1
PUSH 1
MOV AL, 'A'
MOV AH, 0x07
PUSH AX
CALL WriteChar

JMP $


;Write char
;Use:
;   PUSH X-Pos
;   PUSH Y-Pos
;   PUSH Attribute/Color + Char
;        High byte         Low byte

WriteChar:
POP BX ;Attribute/Color + Char
POP CX ;Y-Pos
POP AX ;X-Pos

SHL CX, 40 ;Y-Pos * 80
ADD AX, CX ;X-Pos + (Y-Pos * 80)
SHL AX, 1  ;AX * 2

MOV [EAX+0xB8000], BX ;Write char

RET
;End Write char

Re:Asm help

Posted: Sat Dec 21, 2002 11:16 am
by Slasher
NO!!!
the upper part of EAX could have any value in it!
to ensure that EAX has the same value as AX
clear EAX
mov eax,0
before you assign a value to AX
mov ax,XXXX
now EAX will have same value as AX because
the top part has been filled with ZERO
top EAX bottom EAX
AX
00000000XXXXXXXX

Re:Asm help

Posted: Sat Dec 21, 2002 11:43 am
by Silenger
Ow k thx

What is the fastest MOV EAX, 0 or XOR EAX, EAX?

So:

;Write char
;Use:
; PUSH X-Pos
; PUSH Y-Pos
; PUSH Attribute/Color + Char
; High byte Low byte

WriteChar:
XOR EAX, EAX ;Clear

POP BX ;Attribute/Color + Char
POP CX ;Y-Pos
POP AX ;X-Pos

SHL CX, 40 ;Y-Pos * 80
ADD AX, CX ;X-Pos + (Y-Pos * 80)
SHL AX, 1 ;AX * 2

MOV [EAX+0xB8000], BX ;Write char

RET
;End Write char

Re:Asm help

Posted: Sat Dec 21, 2002 12:37 pm
by Slasher
check out intel's instruction set docs
contain timing info for each instruction

Re:Asm help

Posted: Sat Dec 21, 2002 2:59 pm
by richie
xor eax,eax is faster than mov eax,0 because with xor eax,eax the computer has only to tranfer the operation-code over the system-bus. With mov eax,0 ist first transfers the opcode of mov eax,? and then it transfers the parameter of this operation (in this case it is 0). Another advantage of xor eax,eax is that it is 1 Byte smaller than mov eax,0. (I hope that's right). So your kernel will be one byte bigger if you use mov eax,0, but nowadays this shouldn't matter.

Re:Asm help

Posted: Sat Dec 21, 2002 3:52 pm
by df
personally i've long since given up counting cycles.
i'd rather have

- something that works, over something that doesnt
- a good slow algo instead fast code
etc

Re:Asm help

Posted: Sat Dec 21, 2002 6:18 pm
by Berserk
Why? In things like the BootSector, one byte means alot, so if you could save a byte, why not?

Also, as for speed, i'm not saying that you should spend hours speed optimizing the code, but if you can add a few tweaks that'll save some load time, then why not?

Re:Asm help

Posted: Sun Dec 22, 2002 2:44 am
by Silenger
Thx you all.

So if you mul or div by 2 or 4,6,...
you best use SHL REG, 1 or RHL REG, 1 because its faster and it as easy as writing mul?

Re:Asm help

Posted: Thu Dec 26, 2002 10:24 am
by drizzt
Watch out! if you have to mul by 2, 4, 8, 16... (powers of 2) you can use SHL instruction... you can't use it to multiply by 6...

Re:Asm help

Posted: Thu Dec 26, 2002 5:19 pm
by Curufir
You can perform any multiplication in asm using multiple combitions of SHL and ADD. It's just a question of when the SHL/ADD technique takes up more cycles than the MUL. This varies from processor to processor, MUL is definitely getting to be faster than it used to be. Division of course is an entirely different proposition, if you aren't dividing by 2^X then techniques start to get quite complicated and it's usually better handled by the hardware.

BTW, if you're working in asm it's far handier to be able to do hex maths in your head than decimal or binary.