Tuesday, November 18, 2008

8086 Assembly Language Program to find the factorial upto 255!

Intro
The following assembly language program is for MASM 5.1 up calculates factorial of numbers upto 255!, that is quite a large number to compute a factorial. See below:


Number:255
Factorial
3350850684932979117652665123754814942022
5840635917407025767798842862087990357327
7100562613812676331425928080211850228244
5926550135522251856727692533193070412811
0833303256593220417000297921662507342533
9051375446604571124033846270103402026299
2581378423147276636643647155396305352541
1055414394348401099150682854306750685916
3858198060416294038335658673919826878210
4924614076605793562865241982176207428620
9697768031494674313868079724382476891586
5600000000000000000000000000000000000000
0000000000000000000000000


for the Faint Hearted
The program calculates and computes big numbers by simulating the traditional way of multiplication and division of numbers. Hence it can do this upto any number of digits as much as the memory can hold. In this example, I have used 512 digited numbers, which is more than enough for holding 255!.

The key parts of the following program is bignum_mul and bignum_add macro which simulates the traditional method of multiplying and adding numbers (not using any vedic maths, which can certainly improve the efficiency I guess).

The following is the assembly language program, try digesting...

.asm Program

data segment
b1 db 512 dup(0)
b2 db 512 dup(0)
msg db 0dh,0ah,"Number:$"
fac db 0dh,0ah,"Factorial",0dh,0ah,"$"
data ends

bignum_get macro var
local exit,get,loo,skip
xor cx,cx
lea si,var
mov di,si
mov ah,01h
get:
int 21h
cmp al,0dh
je exit
sub al,30h
mov [si],al
inc si
inc cx
jmp get
exit:
shr cx,1
jz skip
dec si
loo:
mov ah,[si]
mov al,[di]
mov [si],al
mov [di],ah
dec si
inc di
loop loo
skip:
endm

geti macro
local exit,get
xor bx,bx
mov dx,bx
mov cx,00ah
get:
push bx
;Read character
mov ah,01h
int 21h
xor ah,ah
pop bx
;If enter stop
cmp al,0dh
jz exit
sub al,30h
;Multply By 10
push ax
mov ax,bx
mul cx
mov bx,ax
pop ax
;add
add bx,ax
;redo
jmp get
exit:
mov ax,bx
endm

bignum_put macro var
local put,en,nxt,exit
lea si,var
mov di,si
mov cx,0200h
add di,cx
dec di
en:
cmp BYTE PTR [di],00h
jne nxt
dec di
jmp en
nxt:
mov ah,02h
put:
mov dl,[di]
add dl,30h
int 21h
cmp si,di
je exit
dec di
loop put
exit:
endm



bignum_add macro b1,b2
local ader,exit
lea si,b1
lea di,b2

mov cx,0200h
mov bl,10
ader:
mov al,[di]
mov ah,[si]
add al,ah
jz exit
xor ah,ah
div bl
mov [si],ah
inc si
add [si],al
inc di
loop ader
exit:
endm

bignum_mul macro b1
local loo,exit
cmp dl,01h
jz exit
lea si,b1
mov dh,10
xor bx,bx
mov cx,0200h
loo:
mov al,[si]
xor ah,ah
mul dl
add ax,bx
div dh
mov [si],ah
inc si
mov bl,al
loop loo
exit:
endm

puts macro msg
push ax
push dx
mov dx,offset msg
mov ah,09h
int 21h
pop dx
pop ax
endm


assume cs:code,ds:data
code segment
start:
mov ax,data
mov ds,ax

lea si,b1
mov BYTE PTR [si],01h

puts msg
geti
mov dl,al
loo:
push dx
bignum_mul b1
pop dx
dec dl
jnz loo

puts fac
bignum_put b1
mov ah,4ch
int 21h
code ends
end start


The Simple Factorial code upto 8!

geti macro
local exit,get
xor bx,bx
mov dx,bx
mov cx,00ah
get:
push bx
;Read character
mov ah,01h
int 21h
xor ah,ah
pop bx
;If enter stop
cmp al,0dh
jz exit
sub al,30h
;Multply By 10
push ax
mov ax,bx
mul cx
mov bx,ax
pop ax
;add
add bx,ax
;redo
jmp get
exit:
mov ax,bx
endm

puti macro buffer
local exit,put
mov bx,0ah
lea si,buffer
add si,06h
mov BYTE PTR [si],'$'
put:
xor dx,dx
dec si
div bx
add dl,30h
mov [si],dl
cmp ax,0h
jnz put

mov dx,si
mov ah,09h
int 21h

endm

puts macro msg
push ax
push dx
mov dx,offset msg
mov ah,09h
int 21h
pop dx
pop ax
endm

data segment
msg1 db 0dh,0ah,"Enter Number : $"
msg2 db 0dh,0ah,"Factorial : $"
buff db 20 dup(?)
data ends

assume cs:code,ds:data
code segment
start:
mov ax,data
mov ds,ax
puts msg1
geti
push ax
puts msg2
pop ax
mov cx,ax
dec cx
jz over
xor dx,dx
re:mul cx
loop re
over:
puti buff
mov ah,4ch
int 21h
code ends
end start


Thanks to ABA (Bellari) for challenging me to do the 255!.. Yo!!!

Friday, November 7, 2008

Remove old kernels in Ubuntu

Whenever a new kernel is applied ubuntu keeps the old one. This clogs up the grub and hard disk space and ends up having a lot of kernels to boot in the grub.
To know the version that is currently running:

uname -r


and to remove all other kernels use
sudo apt-get remove --purge 2.6.24-16-*


change the 2.6.2x-xx- parts to remove those kernel. Make sure you dont remove the kernel you are using, or you will end up breaking the system. It is wise to keep a recent kernel and a second one just prior to the new one, just in case your system doesn't do well with the new kernel.

Update

Please be sure to type the command exactly as such, dont miss any of the hyphens (-) and dots (.)
Missing something would end up uninstalling all of your kernels and hence making the system not bootable.


Correct commands :
sudo apt-get remove --purge 2.6.27-7-*
sudo apt-get remove --purge 2.6.27-9-*

etc

Command Line Output:

arun@server4:~$ sudo apt-get remove --purge 2.6.27-9-*
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting linux-headers-2.6.27-9-generic for regex '2.6.27-9-*'
Note, selecting linux-image-2.6.27-9-generic for regex '2.6.27-9-*'
Note, selecting linux-image-2.6.27-9-server for regex '2.6.27-9-*'
Note, selecting linux-headers-2.6.27-9 for regex '2.6.27-9-*'
Note, selecting linux-headers-lbm-2.6.27-9-generic for regex '2.6.27-9-*'
Note, selecting linux-headers-2.6.27-9-server for regex '2.6.27-9-*'
Note, selecting linux-restricted-modules-2.6.27-9-server for regex '2.6.27-9-*'
Note, selecting linux-backports-modules-2.6.27-9-generic for regex '2.6.27-9-*'
Note, selecting linux-restricted-modules-2.6.27-9-generic for regex '2.6.27-9-*'
Note, selecting linux-headers-lbm-2.6.27-9-server for regex '2.6.27-9-*'
Note, selecting linux-backports-modules-2.6.27-9-server for regex '2.6.27-9-*'
Note, selecting linux-image-2.6.27-9-virtual for regex '2.6.27-9-*'
The following packages will be REMOVED:
linux-headers-2.6.27-9* linux-headers-2.6.27-9-generic*
linux-image-2.6.27-9-generic* linux-restricted-modules-2.6.27-9-generic*
0 upgraded, 0 newly installed, 4 to remove and 0 not upgraded.
After this operation, 149MB disk space will be freed.
Do you want to continue [Y/n]? y
(Reading database ... 127310 files and directories currently installed.)
Removing linux-headers-2.6.27-9-generic ...
Removing linux-headers-2.6.27-9 ...
Removing linux-restricted-modules-2.6.27-9-generic ...
update-initramfs: Generating /boot/initrd.img-2.6.27-9-generic
Purging configuration files for linux-restricted-modules-2.6.27-9-generic ...
Removing linux-image-2.6.27-9-generic ...
Examining /etc/kernel/prerm.d.
run-parts: executing /etc/kernel/prerm.d/dkms
Uninstalling: nvidia 177.82 (2.6.27-9-generic) (i686)

-------- Uninstall Beginning --------
Module: nvidia
Version: 177.82
Kernel: 2.6.27-9-generic (i686)
-------------------------------------

Status: Before uninstall, this module version was ACTIVE on this kernel.

nvidia.ko:
- Uninstallation
- Deleting from: /lib/modules/2.6.27-9-generic/updates/dkms/
- Original module
- No original module was found for this module on this kernel.
- Use the dkms install command to reinstall any previous module version.
depmod......

DKMS: uninstall Completed.
run-parts: executing /etc/kernel/prerm.d/last-good-boot
Running postrm hook script /sbin/update-grub.
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-2.6.27-11-generic
Found kernel: /boot/memtest86+.bin
Replacing config file /var/run/grub/menu.lst with new version
Updating /boot/grub/menu.lst ... done

The link /vmlinuz.old is a damaged link
Removing symbolic link vmlinuz.old
you may need to re-run your boot loader[grub]
The link /initrd.img.old is a damaged link
Removing symbolic link initrd.img.old
you may need to re-run your boot loader[grub]
Purging configuration files for linux-image-2.6.27-9-generic ...
Running postrm hook script /sbin/update-grub.
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-2.6.27-11-generic
Found kernel: /boot/memtest86+.bin
Updating /boot/grub/menu.lst ... done

rmdir: failed to remove `/lib/modules/2.6.27-9-generic': Directory not empty
dpkg - warning: while removing linux-image-2.6.27-9-generic, directory `/lib/modules/2.6.27-9-generic' not empty so not removed.

Friday, October 24, 2008

When September ends ... October starts

Nothing technical to post this month!
My laptop was sick for most of the days and it got its skeleton (motherboard) replaced. (Freely ofcourse)
Seems to be working fine.

Its a hectic semester! No time to even think what to do next! Universities up in next month.

Waiting for something exciting to happen in my digital world!

Rest of the stuff is really really analog ;)

Sunday, August 10, 2008

Free Online 8085 Assembler and Simulator

Free Online 8085 Assembler and Simulator
I just made this long back, just put it online for anyone that might be interested in using it.

Quick Start

  • Enter the code as shown in the sample in the code view,
  • Click Compile and Simulate.
  • Click HexView to see the hex code of the program
  • Click SimView and Click Step or Exec to simulate the program.


There is a built in special function call for keyboard reads : CALL KBINPUT
Displays a prompt for directly entering into the Accumulator.

In addition memory locations can be assigned in code using
ORG Hex Address
AA BB CC DD
EE FF 00 11
22 33 44 55
66 77
etc

Assembly Source CodeView


HexView


SimView


The SimView Screen is divided into 4 Sections:
  • The extreme left shows the code and highlights the current section being executed.
  • The middle box shows the registers and flags
  • The extreme right shows the stack.
  • The bottom box prints values of memory locations.


More features would be posted as memory comes to me
;)

Friday, August 1, 2008

nVidia Core Temperature too high on my Compaq Presario Lappy

LAPtops as the name stands, conventionally meant to be on the LAP. But in my case not only can it be kept on a lap, it fries everything underneath !
Check out the screenshot!

nVidia GPU Core Temperature Screenshot


This is kinda normal, it gets to 110+ when connected to TV or playing a game!.

The nVidia GeForce card is suspected to do the overheating.
I am using a Compaq Presario V3000 Notebook.
Does any one having a similar series face the problem?
Any Solutions?
Has anyone tried cooking with this lappy? !
Please do comment.

:D