pacman128 / pcasm Goto Github PK
View Code? Open in Web Editor NEWPC Assembly language book
PC Assembly language book
Firstly I understand the difference between lev
and mov
in terms of the result they can achieve, put in simply
mov eax ebp ;put the value in ebp register into eax register
lea eax [ebp] ;same as above, and they are equivalent
However
mov eax,ebp+8 ;invalid register set size
lea eax,[ebp+8] ;calculate in sum of ebp value and 8, then assign it to eax
So what mov eax,ebp+8
is illegal while lea eax,[ebp+8]
is OK? My book says
The value that MOV stores into EAX must be computed by the assembler
(that is, it must in the end be a constant)
But it make no sense to me! What is mean by CONSTANT? The obvious understanding it that the CONSTANT should be calculate by assembler/linker before the program run, BUT, think about mov eax,[ebp+8]
is a LEGAL instruction, The assembler/linker has no way of knowing the value of [ebp+8]
(*(ebp+8) as C lingo) before the program run!
https://stackoverflow.com/questions/77416613/why-leaload-effective-address-is-necessary
@pacman128 First of all, thank you very much for the book, it looks amazing.
As the title says, are there any plans to support 64-bit in the near future? I'm currently teaching myself assembly using my Linux system and I'm facing some issues with some material I have found online.
Some websites teach assembly while using Intel's syntax and some AT&T's, which is confusing me a lot.
By the way, would it be a problem if the book's style change a bit? I'm quite comfortable using LaTeX and one of my eccentricities is producing good-looking ebooks.
Right now I'm trying to mimic the look of this book which has one of the most appealing styles I have ever seen.
Are you open for such change or should I use it for my own version?
Cheers.
Hi,
Just thought I'd share some tips here on how to get the examples working on Windows 10.
For the very first hello.asm
example on Page 25. Run these in batch / command-prompt / Windows Terminal:
rem Set up the 32-bit environment for VC
call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars32.bat"
rem Assemble the hello.asm example
nasm -f win32 -d COFF_TYPE hello.asm
rem Compile & link
cl hello.obj ms/asm_io.obj driver.c legacy_stdio_definitions.lib
In particular linking with legacy_stdio_definitions.lib
was necessary as otherwise _scanf
/ _printf
/ etc cannot be found.
As for running it on WSL (Ubuntu on Win10), it's a bit more complicated. Most distros are 64-bit only, so the user needs to install gcc-multilib
, make sure to link with 32-bit C library, add the ability for the OS to run 32-bit executables, and then finally an additional workaround is necessary to make this all work on WSL.
# Assemble the I/O file (or use the Makefile to assemble everything at once)
cd linux
nasm -felf32 -d ELF_TYPE asm_io.asm
cd ..
# Install GCC
sudo apt install build-essential
# Install 32-bit C standard library
sudo apt-get install gcc-multilib
# Assemble the hello example
# Note: it's not the same as the Windows example, it has different includes, symbols without _, etc.
nasm -felf32 hello.asm
# Compile the driver
gcc -c -m32 driver.c
# Use GCC to link it all together (much easier than using LD directly)
gcc -L /usr/lib32 -lc -m32 linux/asm_io.o driver.o hello-linux.o -ohello-linux
Finally to actually run the 32-bit executable you need additional work. Most of these tips taken from this Stackoverflow answer and from Github:
# Install qemu
sudo apt install qemu-user-static
# Ignore warnings on this one
sudo update-binfmts --install i386 /usr/bin/qemu-i386-static --magic \
'\x7fELF\x01\x01\x01\x03\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x03\x00\x01\x00\x00\x00' \
--mask '\xff\xff\xff\xff\xff\xff\xff\xfc\xff\xff\xff\xff\xff\xff\xff\xff\xf8\xff\xff\xff\xff\xff\xff\xff'
# You MUST run this every time WSL starts. Probably best to add it to your .bashrc / .zshrc
sudo service binfmt-support start
# Add i386 architecture packages
sudo dpkg --add-architecture i386
sudo apt update
sudo apt install g++:i386
And then run the executable:
./hello-linux
Enter a number: 1
Enter another number: 2
Register Dump # 1
EAX = 00000003 EBX = 00000003 ECX = 663025FF EDX = FEFF9A44
ESI = FE7BA000 EDI = FE7BA000 EBP = FEFF99F8 ESP = FEFF99D8
EIP = FEFFB5C7 FLAGS = 0206 PF
Memory Dump # 2 Address = FEFFE1BC
FEFFE1B0 65 72 20 6E 75 6D 62 65 72 3A 20 00 59 6F 75 20 "er number: ?You "
FEFFE1C0 65 6E 74 65 72 65 64 20 00 20 61 6E 64 20 00 2C "entered ? and ?,"
You entered 1 and 2, the sum of these is 3
Finally always make sure to run sudo service binfmt-support start
in WSL on the first start. Probably best to keep it in the .bashrc
/ .zshrc
file in $HOME
.
Hope this helps anyone else trying out this book on Windows.
Really appreciate the free book @pacman128, it's an awesome resource for learning asm!
Cheers!
why the book says the +56(00111000) one's complement is 11000111 on the page 28 (Chapter2)
I think the one's complement and two's complement is the equal to 00111000, because it's a Positive integer
am i right?
first line of 'examples/linux/first.asm' is:
m4_include(asm.m4)
and the content is the same as first.m4
try
push 1;
call sub_dump_regs
push 2;
call sub_dump_regs
push 3
pop eax
pop ebx
pop ecx
Which will introduce a Segmentation fault
The Segmentation fault
will disappear by removing the debugging utilities i.e. sub_dump_regs
push 1;
push 2;
push 3
pop eax
pop ebx
pop ecx
Obvious sub_dump_regs
introduce the problem
Thanks for the Book Paul, very helpful.
While trying to run the first.asm
code, I was not able to compile it under x86_64 Linux. GCC will complain citing architecture incompatibility issues. After a little bit of search I found out that ones needs to add the -m32
option to for it to correctly compile (along with installing gcc-multilib
and g++-multilib
packages before-head)
Also, assembling the asm_io.asm
was not mentioned in section 1.4.3, although it is necessary, and it has a caveat where one needs to add the inline macro -d ELF_TYPE
, this would only be noticed when reading the comments of the asm_io.asm
file. For a beginner, and for his/her very first code, this will hinder a bit, and distract the learner on a quest on the web trying to find a solution.
I have submitted a pull request with some some details on the comments to the asm_io.asm
file.
My system: ubuntu:18.04
Installed texlive texlive-science latex-cjk-english
The following errors have been encountered when using pdflatex pcasm
! Undefined control sequence.
try@size@range ...extract@rangefontinfo font@info
<-*>@nil <@nnil
l.69 \maketitle
?
Is there a solution?
pcasm1.tex
says :
This is one reason why programs written for a Mac can not run on an IBM-type PC.
While I'm sure that this was true at the time the book was written, PowerPC-based Macs are a thing of the past now. Thus, this statement might be a tad misleading to some of the readers, especially seeing how they're expected to be absolute beginners in assembly/machine code related subjects. I suggest changing this to reference mobile phones, since they're probably the most widespread non-x86 devices nowadays, and readers will probably be familiar with them.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.