Recent Posts

Pages: 1 ... 8 9 [10]
91
Programming with NASM / Re: I have a problem with a simple 64bit programm
« Last post by Frank Kotler on February 06, 2019, 02:37:42 AM »
Hi Renegade,

Welcome to the Forum!

To start with, replace "global _start" with "global main". I think that will make it assemble and link. There may be other issues.

Best,
Frank

92
Programming with NASM / I have a problem with a simple 64bit programm
« Last post by Renegade on February 06, 2019, 02:03:16 AM »
Hey guys,
I wrote a code to search for primenumbers in a given range.
My programm shall take an argument, which will be the last number checked.
To accelerate the code i wanna check divisibility through 2,3,5,7 before i increase the divisor.
If the divisor reaches the current number x/2 you can be sure that it is prime.

I am sorry bothering here for help, but i don't understand whats wrong with my code, because it gives my an errormessage while linking.
Also do i want to find large primenumbers > 1000000 etc....
Therefore i am not sure if i declared the size of "output" correctly.


Code: [Select]

; Compiled with
; nasm -f elf64 prime.asm && gcc -o prime prime.o

global _start
extern atoi
extern printf
extern puts

section .text

main:
cmp rdi, 2 ;check if it has one argument
jne error1
xor rdi, rdi

mov rdi, rsi; argument
call atoi wrt ..plt
mov r8, rdi
inc r8
xor rdi, rdi
mov r9, 2 ;number to begin
mov r11, 11

checkPrime2: ; check if divisible by 2
xor rdx, rdx
cmp r8, r9
je done
mov rax, r9
mov rcx, 2
div rcx
mov r10, rax
cmp rdx, 0
je notPrime

checkPrime3: ; check if divisible by 3
xor rdx, rdx
mov rax, r9
mov rcx, 3
div rcx
cmp rdx, 0
je notPrime

checkPrime5: ; check if divisibly by 5
xor rdx, rdx
mov rax, r9
mov rcx, 5
div rcx
cmp rdx, 0
je notPrime

checkPrime7: ; check if is divisible by 7
xor rdx, rdx
mov rax, r9
mov rcx, 7
div rcx
cmp rdx, 0
je notPrime

; increase divisor until it reaches half of the given number.
; r11 is the divisor which will be incremented
; in r10 is the actual number divided by two
divisorIncrease:
xor rcx, rcx
mov rax, r9
mov rcx, r11
div rcx
cmp rdx, 0
je notPrime
cmp r11, r10
je printPrime
inc r11
jmp divisorIncrease

printPrime:
mov rdi, output
mov rsi, r9
call printf wrt ..plt
inc r9
cmp r8, r9 ; r8 contains the argument (highest number) +1
je done
jmp checkPrime2

notPrime:
inc r9
cmp r8, r9
je done
jmp checkPrime2


error1:
mov rdi, noArgument
call puts wrt ..plt

done:
ret



section .data

output:
dq "%d", 10, 0

noArgument:
db "Requires exactly one argument (last number to check)", 10, 0



I try to compile and link this code to make an executable, but everytime i get this error:



nasm -f elf64 prime.asm && gcc -static -m64 prime.o -o prime
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/crt1.o: in function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status



Best Regards and very gracefully,
Renegade ;)
93
Programming with NASM / Re: Example INT 0x10 AH 0x0E in Bios
« Last post by debs3759 on February 05, 2019, 11:21:29 PM »
With a properly written IBM compatible BIOS that should work. Can you use a debugger to single step through the code while in the VM, to see if it is changing any registers?
94
Programming with NASM / Re: Example INT 0x10 AH 0x0E in Bios
« Last post by Frank Kotler on February 05, 2019, 09:38:49 PM »
Hi  ramosaurio,

Welcome to the Forum.

I would expect your code to work on real hardware. Who knows what a VM might do? Try reloading ah with 0x0E before each interrupt.

Best,
Frank

95
Programming with NASM / Example INT 0x10 AH 0x0E in Bios
« Last post by ramosaurio on February 05, 2019, 08:20:15 PM »
Hello everyone, i just started to learn programming with nasm. I'm doing a little program to verify the Bios interrupt set.
I have experience programming with ARM, but now i'm stuck with the 0x10 interruption. I did a simple "Hello" to print it with the 0X0E mode.
Code: [Select]
[BITS 16]

org 0x7c00
mov ah, 0x0e
mov al, 'H'
int 0x10
mov al, 'E'
int 0x10
mov al, 'L'
int 0x10
int 0x10
mov al, 'O'
int 0x10

jmp $

times 510 - ($-$$) db 0
dw 0xaa55


I take this example from this Bootloader project https://github.com/cfenollosa/os-tutorial/tree/master/02-bootsector-print

The problem is that when i mount it in my VM it only prints the first lettler but not the others letters. 

Thank's in advantage.

PD. I think that i post it in the correct section, sorry if there are any problem.
96
Programming with NASM / Re: What's wrong with this code?
« Last post by debs3759 on February 02, 2019, 05:56:06 PM »
I think port 92 is independent from the KBC. It's certainly much faster than the older method, but not supported on all older systems. This link will tell you more: https://www.win.tue.nl/~aeb/linux/kbd/A20.html
97
Programming with NASM / Re: What's wrong with this code?
« Last post by ben321 on February 02, 2019, 04:43:59 AM »
Now the next step is to figure out the easiest (least lines of code) way to enable the A20 line, for COMPLETE access to the full 32bit memory space.

My code to test and enable A20 is:

Code: [Select]
%define KBC_Control 060h ; KBC Control Port
%define KBC_Status 064h ; KBC Status Port

%define KBC_in_buf_full 002h ; Input buffer full
%define KBC_enable_A20 0DFh ; enable A20 command


;-------------------------------------------------------------------------------+
; EnableA20: +
; +
; input: none +
; +
; output: CF A20 is set +
; NC A20 is not set +
; +
; This routine first tests whether A20 is set. If not, it tries to set it using +
; Port 92 (Fast A20). If that fails, it then writes to the keyboard +
; controller to set it. +
; +
; If A20 is enabled the routine returns with CF set, else CF is cleared. +
;-------------------------------------------------------------------------------+

section .text

EnableA20:
push ax
push dx

call TestA20 ; is A20 set?
jc .1 ; yes, exit

in al,092h ; read Port 92h
or al,02h ; Set A20 bit
out 092h,al ; write it - set A20

call TestA20 ; Test A20 again...
jc .1 ; if set, exit

mov ah,KBC_enable_A20
call KBC_GateA20

call TestA20 ; Test A20 again...

.1:
pop ax
pop dx

ret

;-------------------------------------------------------------------------------+
; TestA20: +
; +
; input: none +
; +
; output: CF A20 is set +
; NC A20 is not set +
; +
; This routine test whether A20 is set or not. +
;-------------------------------------------------------------------------------+

section .text

TestA20:
push ds
push es

push ax ; used a few times...
push bx ; likewise....
push si
push di

xor si,si
xor di,di
mov ds,si ; DS:SI points to Int 0 vector

mov ax,0FFFFh
mov es,ax
mov di,010h ; ES:DI points to:
; 0x0000  if A20 not set
; 0x10000 if A20 is set
cli

push word [DS:0] ; save Int 0 vector

lodsw ; ax contains int 0 vector
mov bx,ax ; save in bx
dec ax ; decrement ax
stosw ; and save to ES:DI

xor si,si
xor di,di
lodsw ; read DS:SI again

cmp ax,bx ; if A20 is set, ZF is set

pop word [DS:0] ; restore Int 0 vector

sti

clc
jnz .1 ; if ZF
stc ; then set CF
.1:

pop di
pop si
pop bx
pop ax

pop es
pop ds

ret

;-------------------------------------------------------------------------------+
; KBC_Wait: +
; +
; input: none +
; +
; output: NC = buffer is empty +
; CF = buffer is not empty +
;-------------------------------------------------------------------------------+

section .text

KBC_Wait:
push ax ; save ax
push bx ; save bx
push cx ; save cx
mov bx,5 ; set super-long timeout
xor cx,cx ; cx=0:  timeout value
.1:
out 0edh,ax ; I/O delay
in al,KBC_Status ; read 8042 status port
and al,KBC_in_buf_full ; input buffer full flag (D1)
loopnz .1 ; loop until input buffer empty
;   or timeout
jz .2 ; success!
dec bx ; are we done yet?
jnz .1 ; keep trying
or al,al ; success on last try?
.2:
clc
jz .3
stc
.3:
pop cx ; restore cx
pop bx ; restore bx
pop ax ; restore ax
ret

;-------------------------------------------------------------------------------+
; KBC_GateA20: +
; +
; input: ah = command to send to KBC +
; +
; output: NC = Command succeeded +
; CF = Command failed +
;-------------------------------------------------------------------------------+

section .text

KBC_GateA20:
pushf ; save interrupt status
cli ; disable ints while using 8042

Call KBC_Wait ; insure 8042 input buffer empty
jc A20_Fail ; ret: 8042 unable to accept cmd

out 0edh,ax ; I/O delay
mov al,0D1h ; 8042 cmd to write output port
out KBC_Status,al ; output cmd to 8042

Call KBC_Wait ; wait for 8042 to accept cmd
jc A20_Fail ; ret: 8042 unable to accept cmd

mov al,ah ; 8042 port data
out KBC_Control,al ; output port data to 8042

Call KBC_Wait ; wait for 8042 to port data
jc A20_Fail

push cx ; save CX
mov cx,14h ;
.DLY:
out 0edh,ax ; Wait for KBC to execute the
loop .DLY ;  command.  (about 25uS)
pop cx ; restore CX

clc

A20_Fail:
popf ; restore flags
ret

It's not compact, as it contains lots of checks, but if you need to remove all the checks to make it more compact, that should be easy enough. The I/O delays are needed on some systems, as without it your code may be too fast for the KBC controller.

Is there a way to do it without the keyboard controller? Given that Protected 32bit mode is an intentional feature in the CPU, you'd think that Intel would have given a more direct route to activate this mode, than require you to do a hacky thing with the KBC. I mean, I'd think they'd make it so activating 32bit protected mode automatically activated the A20 gate, since there is no circumstance in which you would need to run 32bit protected mode with A20 gate disabled (the whole A20 gate thing is only for 16bit legacy modes anyway). In 32bit protected mode, you will always want to activate the A20 gate.

And even if the A20 gate requires a separate action to activate it, you'd think that this Intel would create an opcode directly in the CPU for this VERY IMPORTANT operation, so one line of assembly code could activate the A20 gate. Or at the very least you'd think it would be something that could be activated via the correct output port using an OUT instruction, or via the correct BIOS interrupt with the INT instruction.
98
Programming with NASM / Re: What's wrong with this code?
« Last post by debs3759 on February 02, 2019, 12:12:15 AM »
Now the next step is to figure out the easiest (least lines of code) way to enable the A20 line, for COMPLETE access to the full 32bit memory space.

My code to test and enable A20 is:

Code: [Select]
%define KBC_Control 060h ; KBC Control Port
%define KBC_Status 064h ; KBC Status Port

%define KBC_in_buf_full 002h ; Input buffer full
%define KBC_enable_A20 0DFh ; enable A20 command


;-------------------------------------------------------------------------------+
; EnableA20: +
; +
; input: none +
; +
; output: CF A20 is set +
; NC A20 is not set +
; +
; This routine first tests whether A20 is set. If not, it tries to set it using +
; Port 92 (Fast A20). If that fails, it then writes to the keyboard +
; controller to set it. +
; +
; If A20 is enabled the routine returns with CF set, else CF is cleared. +
;-------------------------------------------------------------------------------+

section .text

EnableA20:
push ax
push dx

call TestA20 ; is A20 set?
jc .1 ; yes, exit

in al,092h ; read Port 92h
or al,02h ; Set A20 bit
out 092h,al ; write it - set A20

call TestA20 ; Test A20 again...
jc .1 ; if set, exit

mov ah,KBC_enable_A20
call KBC_GateA20

call TestA20 ; Test A20 again...

.1:
pop ax
pop dx

ret

;-------------------------------------------------------------------------------+
; TestA20: +
; +
; input: none +
; +
; output: CF A20 is set +
; NC A20 is not set +
; +
; This routine test whether A20 is set or not. +
;-------------------------------------------------------------------------------+

section .text

TestA20:
push ds
push es

push ax ; used a few times...
push bx ; likewise....
push si
push di

xor si,si
xor di,di
mov ds,si ; DS:SI points to Int 0 vector

mov ax,0FFFFh
mov es,ax
mov di,010h ; ES:DI points to:
; 0x0000  if A20 not set
; 0x10000 if A20 is set
cli

push word [DS:0] ; save Int 0 vector

lodsw ; ax contains int 0 vector
mov bx,ax ; save in bx
dec ax ; decrement ax
stosw ; and save to ES:DI

xor si,si
xor di,di
lodsw ; read DS:SI again

cmp ax,bx ; if A20 is set, ZF is set

pop word [DS:0] ; restore Int 0 vector

sti

clc
jnz .1 ; if ZF
stc ; then set CF
.1:

pop di
pop si
pop bx
pop ax

pop es
pop ds

ret

;-------------------------------------------------------------------------------+
; KBC_Wait: +
; +
; input: none +
; +
; output: NC = buffer is empty +
; CF = buffer is not empty +
;-------------------------------------------------------------------------------+

section .text

KBC_Wait:
push ax ; save ax
push bx ; save bx
push cx ; save cx
mov bx,5 ; set super-long timeout
xor cx,cx ; cx=0:  timeout value
.1:
out 0edh,ax ; I/O delay
in al,KBC_Status ; read 8042 status port
and al,KBC_in_buf_full ; input buffer full flag (D1)
loopnz .1 ; loop until input buffer empty
;   or timeout
jz .2 ; success!
dec bx ; are we done yet?
jnz .1 ; keep trying
or al,al ; success on last try?
.2:
clc
jz .3
stc
.3:
pop cx ; restore cx
pop bx ; restore bx
pop ax ; restore ax
ret

;-------------------------------------------------------------------------------+
; KBC_GateA20: +
; +
; input: ah = command to send to KBC +
; +
; output: NC = Command succeeded +
; CF = Command failed +
;-------------------------------------------------------------------------------+

section .text

KBC_GateA20:
pushf ; save interrupt status
cli ; disable ints while using 8042

Call KBC_Wait ; insure 8042 input buffer empty
jc A20_Fail ; ret: 8042 unable to accept cmd

out 0edh,ax ; I/O delay
mov al,0D1h ; 8042 cmd to write output port
out KBC_Status,al ; output cmd to 8042

Call KBC_Wait ; wait for 8042 to accept cmd
jc A20_Fail ; ret: 8042 unable to accept cmd

mov al,ah ; 8042 port data
out KBC_Control,al ; output port data to 8042

Call KBC_Wait ; wait for 8042 to port data
jc A20_Fail

push cx ; save CX
mov cx,14h ;
.DLY:
out 0edh,ax ; Wait for KBC to execute the
loop .DLY ;  command.  (about 25uS)
pop cx ; restore CX

clc

A20_Fail:
popf ; restore flags
ret

It's not compact, as it contains lots of checks, but if you need to remove all the checks to make it more compact, that should be easy enough. The I/O delays are needed on some systems, as without it your code may be too fast for the KBC controller.
99
Using NASM / Re: extraneous symbol 0000000000000000 t ..@13.end
« Last post by Olsonist on February 01, 2019, 06:48:00 PM »
> Odd. Does it do any harm? Does "end" occur anywhere in xxx.s?

I don't know that it does any harm. It just seems to be a symbol. "end" doesn't occur anywhere in the source.

(EDIT: if I comment out %use smartalign and ALIGNMODE below, the symbol disappears.)

I've chopped the source back to practically nothing:

SECTALIGN 4096

%use smartalign
ALIGNMODE p6,16
BITS 64
default rel

section .text

align 4096
_xxx:
nop

I'm using OSX 10.14.2 and NASM version 2.14.02 compiled on Dec 27 2018.

nasm -f macho64 xx.s -o xx.o

nm xx.o
0000000000000000 t ..@8.end
0000000000000000 t _xxx

Strange. BTW, if I substitute ELF64 and readelf xx.o I get the same result:

nasm -f elf64 xx.s -o xx.o
readelf -s xx.o

Symbol table '.symtab' contains 5 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS xx.s
     2: 0000000000000000     0 SECTION LOCAL  DEFAULT    1
     3: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT    1 ..@8.end
     4: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT    1 _xxx
100
Announcements / Re: Setting Up Insight Debugger on Ubuntu 16.04 LTS 64 Bit
« Last post by bagore on February 01, 2019, 02:22:09 PM »
SSC CHSL Recruitment 2019  notification for the recruitment of various posts such as Postal & Sorting Assistant, Lower Divisional Clerk, and Data Entry Operator.
Pages: 1 ... 8 9 [10]