Assembly Language Square Values
What would be the most efficient way to take an input 'x' and find the respective 'x^2' (of which is stored in another variable) with only the basic assembly operators? (Add, Sub, Store, etc.)
See also questions close to this topic
Printing Commandline Arguments x86 Assembly
First off I'm new to assembly but have a decent background in c++. My problem stems from a tutorial I was following on Commandline arguments using assembly.
When the program is run it does everything it is suppose to do according to the tutorial, which is display the list of program inputs:
./asm1 arg1 arg2 arg3
The concern is when you provide an astrisk as the argument:
It effectively does a /usr/bin/ls on the CWD.
My question is how do I prevent this, and any advice on a better way to implement this program.
; the loop cmp ecx, eax ; if ecx not equal to eax jne begin_for_loop ; jmp to loop, else exit
; the loop cmp ecx, eax ; if ecx not equal to eax jmp begin_for_loop ; jmp to loop, else exit
And I found a bigger issue where the program prints ENV variables to the screen.
Here's the original program below:
; FOR LOOP: Print commandline arguments> an equivalent program to this in assembly SECTION .data argv db "Arguments = %s",10,0 argc db "Argument Count = %d",10,0 SECTION .text ; allow access to printf extern printf ; make main_function available externally global main main: ; int main (int argc, char* argv) push ebp mov ebp,esp sub esp, 10 mov eax, DWORD [ebp + 8] ; points to argc mov ebx, DWORD [ebp + 12] ; points to argv mov ecx, 0 ; mov ZERO to count register begin_for_loop: ; always preserve values prior to external function calls ; external function calls may modify values you placed in registers push ebx ; preserve ebx; holds argument address push eax ; preserve eax; holds number of arguments push ecx ; preserve ecx; holds the counter ; call printf push DWORD [ebx] push argv call printf add esp, 8 ; clean up the stack ; always restore in backwards order pop ecx ; restore counter pop eax ; restore number of arguments pop ebx ; restore argument address inc ecx ; increase our counter by 1 add ebx, 4 ; move to next argument in the array ; the loop cmp ecx, eax ; if ecx not equal to eax jne begin_for_loop ; jmp to loop, else exit mov esp,ebp pop ebp ret
$ ./asm5 me you them us Arguments = ./asm5 Arguments = me Arguments = you Arguments = them Arguments = us
$ ./asm5 me you them us * Arguments = ./asm5 Arguments = me Arguments = you Arguments = them Arguments = us Arguments = asm1 Arguments = asm1.asm Arguments = asm1.o Arguments = asm2 Arguments = asm2.asm Arguments = asm3 Arguments = asm3.asm Arguments = asm4 Arguments = asm4.asm Arguments = asm5 Arguments = asm5.asm Arguments = asm-template.asm Arguments = compile-asm.sh Arguments = cpp_libs
Making a mousehandler in x86 assembly
I'm writing an Operating System in NASM assembly and I have a problem with making a mouse handler (POINTING DEVICE BIOS INTERFACE handler). I have tried to make one for a week now, but I haven't succeeded yet
I've tried searching on the internet on how to do it, but that didn't help me so much.
Here is my set up code in the beginning of the code:
call checkPS2 ;PS2 routines jc NOMOUSE call enablePS2 jc NOMOUSE
And here is the PS2 functions
;PS2 mouse routines NOMOUSE: ;Prompt error ret checkPS2: int 11h ; get equipment list test al, 3 jz noPS2 ; jump if PS/2-Mouse not indicated mov bh,3 mov ax, 0C205h int 15h ; initialize mouse, bh=datasize jc noPS2 mov bh,3 mov ax, 0C203h int 15h ; set mouse resolution bh jc noPS2 mov ax, cs mov es, ax mov bx, PS2dummy mov ax, 0C207h int 15h ; mouse, es:bx=ptr to handler jc noPS2 xor bx, bx mov es, bx ; mouse, es:bx=ptr to handler mov ax, 0C207h int 15h ret noPS2: stc ret PS2dummy: retf enablePS2: call disablePS2 mov ax, cs mov es, ax mov bx, IRQhandler mov ax, 0C207h ; es:bx=ptr to handler int 15h mov bh,1 ; set mouse on mov ax, 0C200h int 15h ret disablePS2: xor bx, bx ; set mouse off mov ax, 0C200h int 15h xor bx, bx mov es, bx mov ax, 0C207h ; es:bx=ptr to handler int 15h ret IRQhandler: ;assume ds:nothing,es:nothing cld push ds push es pusha mov ax, cs mov ds, ax mov bp,sp mov al,[bp+24+6] ; buttons mov bl,al shl al,3 ; CF=Y sign bit sbb ch,ch ; signed extension 9->16 bit cbw ; extend X sign bit mov al,[bp+24+4] ; AX=X movement mov cl,[bp+24+2] ; CX=Y movement xchg bx,ax neg cx ; reverse Y movement popa pop es pop ds retf
My loop that calls those functions
mov ax, 0xc209 ;Reads from the mouse int 15h ;-||- mov dl, bl ;first byte(bl) mov bl, 02h ;Color Green call printhex ;prints dl in hex mov ax, 0xc209 ;Reads from the mouse int 15h ;-||- mov dl, cl ;second byte(bl) mov bl, 02h ;Color Green call printhex ;prints dl in hex mov ax, 0xc209 ;Reads from the mouse int 15h ;-||- mov bl, 02h ;Color Green call printhex ;prints dl(3rd byte read)
The first thing I noticed was that there was many white dots on the screen that just came up randomly and that has never happened before.
The code i did should just give me the location of the mouse while the mouse cursor should remain hidden. I had already tested my
printhexfunction and I know that it worked and the numbers I saw was:
0x02 0x00 0x00
When I moved the mouse none of the values changed.
The configurations I had was VirtualBox with a PS/2 mouse.
Change the bitmap via x86 assembly
I try to modify the bitmap - add a pixel on it.
push ebp mov ebp, esp put_pixel: mov eax, DWORD [ebp+8] ;address of bitmap add eax, 130 ;take address i wanna change mov eax, [eax] ;load address not eax ;this adders is ff(white) change to (00) black mov ecx, eax ;return value mov eax, ecx ;return ecx pop ebp ret
But it does not do a thing. Is it a proper way of changing the pixel color on the bitmap? Maybe should I save it somehow?
How to develop little man computer program to check if a number is fibonacci?
I am working on writing a program in little man computer to check if a number is a fibonacci number. Does anyone have any ideas?
Little Man Computer?
Little Man Computer making the user for three numbers and then repeat them in reverse order.
Test data: Expected output: 7, 8, 9 9, 8, 7 16, 12, 5 5, 12, 16
Does anyone know the code?
My little man computer code is stuck in a loop
The original objective was to create a program using a little man computer. I thought I had it working, but I believe its stuck in the loop I have. Can anyone see where I went wrong?
Here's what I have:
INP ; 00 901 input p STO p ; 01 308 store p INP ; 02 901 input q BRZ end ; 03 705 while q > 0 BRP loop ; 04 810 end LDA p ; 05 508 result is p OUT ; 06 902 HLT ; 07 000 p DAT ; 08 q DAT ; 09 loop SUB p ; 10 208 compute q - p in calculator BRZ end ; 11 705 BRP loop ; 12 810 if q > p, q := q - p ADD p ; 13 108 else STO q ; 14 309 LDA p ; 15 508 SUB q ; 16 209 STO p ; 17 308 p := p - q LDA q ; 18 509 BRA loop ; 19 610