Questions tagged [assembly]
An assembly language is a low-level programming language for a computer, or other programmable device, in which there is a very strong (generally one-to-one) correspondence between the language and the architecture's machine code instructions.
305
questions
107
votes
10answers
41k views
Fastest way to clamp an integer to the range 0-255
I'm working on some image processing code that can generate pixel values outside of the normal range of 0 to 255, and I'd like to clamp them back into the valid range. I know that there are saturating ...
76
votes
2answers
9k views
Brainfuck Interpreter written in x86 Assembly
Story time: A week ago I found a question about optimizing Assembly code, then I remembered how awesome Brainfuck was, and the match was made very quickly. I decided to write a Brainfuck Interpreter ...
48
votes
2answers
3k views
16-bit FizzBuzz in x86 NASM assembly
Since this problem involves small numbers (particularly with a small loop count of 100), it's possible to ease the modulo operation setup by simply working with 16-bit and 8-bit registers:
$$\dfrac{\...
42
votes
5answers
3k views
The loaderless bootloader
I began this month with trying to discover how shellcode is made. My search led me to wanting to learn assembly, so I wrote a simple bootloader with NASM:
...
40
votes
2answers
1k views
Brainfuck to x86 Assembly Compiler
After my Brainfuck Interpreter written in x86 Assembly I decided that it was time to get to the next step, writing a Brainfuck Compiler in Java that generates x86 Assembly and compiles that to an ...
36
votes
2answers
12k views
Counting Sequence Length in TIS-100
I picked up a programming game, TIS-100. Programming manual can be found on Steam as well, but I have described the relevant syntax in my question.
Basically, you're dealing with some old machine ...
32
votes
3answers
6k views
Decide if the sum of three numbers is even or odd
Exercise from an Assembly course I'm enrolled into:
Write a program that takes three numbers x,y,z as input, and returns:
0 if (x+y+z) is even.
1 if (x+y+z) is odd.
(Note that ...
27
votes
2answers
2k views
Euler's Totient Function in x86 assembly (MASM)
My task was to implement Euler's totient function, and I'm looking for any and all criticism.
...
27
votes
4answers
7k views
Fast 32x32 bit multiply on ARM M0
I have a time-critical calculation running on an ARM Cortex-M0. This is the fastest 32x32 bit -> 64 bit multiplication that I can come up with.
The following C compiles almost 1-1 to assembly ...
27
votes
1answer
2k views
Stack implemented using linked list in x86_64 assembly
I wrote a stack implementation using a singly linked list in x86_64 assembly. This stack supports the usual push/pop operations ...
24
votes
3answers
5k views
Checking if a number is prime in NASM Win64 Assembly
I'm just learning Assembly. This program is a simple attempt to determine if a given number is prime or not.
Compiled as follows using VS2019 x64 Native Tools Command Prompt:
...
24
votes
4answers
2k views
x86-64 Assembly - Sum of multiples of 3 or 5
I'm trying to learn some basic x86 assembly and so I've begun solving Project Euler problems. I was hoping for some critique of my code that, hopefully, includes either the efficiency of the ...
19
votes
0answers
295 views
Multiplying big numbers using Karatsuba's method
The Karatsuba algorithm, first published in 1962, aims to speed up the multiplication of big numbers by reducing the number of 'single-digit-multiplications' involved.
Because of its complexity (...
18
votes
2answers
3k views
GCD/LCM calculator in x86 NASM assembly
After creating a similar program in C++, I've decided to try to write it in x86 assembly language (the type of assembly I was taught in college).
I'd like to receive feedback regarding syntax and ...
17
votes
1answer
783 views
Calculating the harmonic average
In this program I was required to calculate the Harmonic Average using an assembly program with a C driver.
Harmonic mean is defined as:
$$ \frac{n}{\dfrac{1}{x_1} + \dfrac{1}{x_2} + \cdots + \dfrac{...
17
votes
1answer
312 views
Brainfreeze: A Brainfuck compiler in C
BrainFreeze
This is my implementation of a brainfuck compiler in C. The instruction to build/run the project (GitHub repo) can be found here.
I am first converting the brainfuck source to x86 ...
16
votes
2answers
684 views
x64 assembly clearmem / zeromem
I've just started learning assembly yesterday, and the first useful thing I've written is a clearmem function.
I'm looking for general feedback regarding my coding ...
15
votes
3answers
1k views
Reverse Polish notation based compiler
Description
Very small subset of Forth
This is a proof of concept level compiler, no optimizations or over/underflow checking
See the embedded POD for more information
NASM is used as assembler
gcc ...
15
votes
1answer
532 views
Signed integer-to-ascii x86_64 assembler macro
I have implemented an algorithm which converts a signed integer to its ASCII equivalent string with the corresponding sign as a first byte. I have tested it on several numbers and it worked correctly. ...
14
votes
2answers
1k views
Parsing Z80 assembler in C++
I'm just starting with C++ (I have worked with C for more than a year) and as a first program I wanted to write a cleaner version of my Z80 assembler I had written in C before, this time trying to do ...
14
votes
1answer
362 views
Embedded FizzBuzz
Recently, I have started to enter the realms of embedded systems programming. And, as my first major project, I thought I'd do the obvious: FizzBuzz.
However, this is a little different: this is a ...
14
votes
1answer
3k views
Linux NASM assembly linked list implementation
Inspired by all of the lovely linked lists lately, I decided to implement one in assembly language. This code maintains two linked lists - one is a free store and the other is the active linked list. ...
13
votes
4answers
2k views
Countdown program in x86 NASM
I am fairly new to Assembly language programming and, for practice, I gave myself a problem: count down from 10 and right after 1, say "Blast off!".
...
13
votes
3answers
2k views
Inefficient differential converter
Following Pimgd's question, I decided to take a look at the TIS-100 myself.
This is my solution to the 3rd challenge, building a differential converter.
Requirements:
Read values from IN.A and IN....
13
votes
2answers
615 views
A low tech approach to measuring game speed
Whilst developing a game I needed a delay routine capable of doing delays
ranging from 0.5 sec to just a few msec. The obvious choice was to use
the BIOS delay function 86h on int 15h. In a true real ...
13
votes
2answers
162 views
Calculating (\$a^2\$ + \$b(2c-d)^2)/(3e)\$ in assembly
I made a simple assembly program to evaluate the function
$$f =\frac{a^2 + b(2c-d)^2}{3e}$$
Since this is basically my first ever, I would like to hear what I can improve and know how to use ...
13
votes
2answers
1k views
Brainf*ck interpreter written in x86 assembly
I have written a program in x86 assembly (Intel syntax/MASM) that interprets brainfuck code that is fed to it via an interactive console and prints the final stack to stdout. Note that it does not ...
13
votes
2answers
438 views
Find binary sequences with low peak sidelobe level of autocorrelation function
I would like to find all binary sequences from the specified range, which has low peak sidelobe level of its autocorrelation function.
Here is the solution (this is simplified version of the real ...
13
votes
1answer
310 views
Loading a file from a FAT32 filesystem in 16 bit real mode on x86 (Intel syntax assembly)
I am writing a bootloader that needs to load a file from a FAT32 filesytem. Since I need to load both the root directory first to find the start of the file, and then the actual file, I wrote a ...
13
votes
1answer
189 views
Brainfuck to NASM compiler in Haskell
After reading this article on writing a Brainfuck interpreter in Haskell and achieving awful performance with it (for example, mandelbrot generating 37.5 minutes on an old Intel Atom powered laptop) I'...
12
votes
2answers
1k views
x64 Assembly - checking for largest prime factor
Using x64 assembly, I solved the Largest Prime Factor problem on Project Euler.
The problem is as follows:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of ...
11
votes
3answers
2k views
Decoding assembly instructions in a Game Boy disassembler
I am coding a game boy disassembler in C++. The program (simplified here) converts a vector of byte into an assembly instruction and print it.
To do so, it iterates through the vector of bytes (char)...
11
votes
4answers
1k views
Detecting arithmetic overflow in C with NASM
This snippet is about detecting whether the carry flag is set or cleared. I work on Mac OSX, so my snippet supports Mac only.
First, we need a routine that does the job:
func.s:
...
11
votes
3answers
13k views
Checking substring in 8086 ASM
I have tried like this to check a substring in a mainstring in 8086. Is there any shorter way of doing this? My implementation seems lengthy.
...
11
votes
1answer
2k views
string length in x64 assembly (fasm)
Please critique this very, very basic routine which returns the length of a given char buffer or "string."
...
11
votes
1answer
3k views
ToyVM - a small and simple virtual machine in C - follow-up
(See the previous and initial iteration)
Now I have refactored the code in order to conform to the suggestions made in the answers to the first iteration. I have this:
toyvm.h:
...
11
votes
1answer
492 views
Assembler for CPU
I recently put together an assembler for a CPU I designed. I'm looking for feedback on my program structure, formatting, or anything else. I'm self taught on all of this so I don't have opportunities ...
10
votes
3answers
251 views
Distance between two n-dimensional points (NASM)
I just finished writing a function that computes the distance between two n-dimensional points.
The original one was written in C and it's basically a translation of this formula:
\$\text{dist}(x,y)=...
10
votes
3answers
424 views
SSE loop to walk likely primes
This is a continuation of a discussion that was started here. While there are some interesting points there about instruction timing and latency, it is not necessary to read that Question to ...
10
votes
2answers
7k views
Assembly beginner exercise: Calculate the square numbers from 1 to n
I try to learn (flat-, FASM-) Assembly currently.
For to become more familiar with the concept of branching I made a tiny program. Inspired by an exercise I had to do once in school when learning ...
10
votes
2answers
345 views
Disassembler for intel 8080
I have written a disassembler for Intel 8080, able to translate binaries into opcodes and parameters. Sorry for posting so much code, but everything posted here is useful for the understanding of the ...
10
votes
1answer
1k views
Measuring the clock rate of an Intel CPU in C++
I created a short program to directly measure the clock rate of an Intel CPU. It runs, sleeps for a duration specified by the user, and then measures the number of clock cycles and the amount of time ...
10
votes
2answers
1k views
Dijkstra's algorithm for computing the GCD of two integers, translated from C to x86 assembly
This is the x86 version of a C file assignment (I have included both). We were to convert the C code to assembly. I am rather new at assembly and would really appreciate suggestions and help for ...
10
votes
2answers
418 views
10
votes
1answer
4k views
A Virtual Piano
I have created a virtual piano that turns the home row (excluding 'g' and 'h') and the top row (excluding 'y' and 'u') of the user's keyboard in a piano's keyboard.
The home row is used for white ...
10
votes
1answer
255 views
A 8086 assembly hexdump program
I am trying to learn the 8086 assembly language. So as exercise, I decided to write a hexdump program, that outputs the contents of files in a format similar to Linux hexdump.
I am developing under ...
10
votes
1answer
234 views
A BrainF*ck-ish compiler in C
-ish because I removed the input functionality from the compiler. And, the compiler does not support nested loops
I've been recently reading up on compilers and how they work. Although this doesn't ...
10
votes
1answer
5k views
Integer-to-ASCII algorithm (x86 assembly)
This is my best effort at converting a 32 bit integer in EAX, to an 8 character ascii string (result in RDI). It will work ...
9
votes
2answers
327 views
ToyVM - a small and simple virtual machine in C + FizzBuzz demonstration
(See also the next iteration.)
I have this small virtual machine.
What is there:
Stack and, thus, recursion.
Conditional and unconditional jumps and, thus, choice and iteration.
What is not there:...
9
votes
3answers
6k views
x86 strcpy implementation
I got about 4 days of assembly knowledge, so I need a review on this strcpy function and if it can be done better (at least I have the feeling).
Full code (with ...