Giter Club home page Giter Club logo

low-level-programming's Introduction

Apress Source Code

This repository accompanies Low-Level Programming by Igor Zhirkov (Apress, 2017).

Cover image

Download the files as a zip using the green button, or clone the repository to your machine using Git.

Student projects

Each year, my students in ITMO university can opt to make a project that is connected to system programming or system programming languages in general instead of passing the final test. Each project is coded by one student, who is is free to chose a topic of interest. The source code is always available on GitHub and video demonstrations are mandatory.

The videos are being uploaded to our YouTube channel. Soon all available videos for all years are going to be uploaded.

Repository structure

This repository holds the following information:

  1. recommended if you are new to Linux Ubuntu 16 with standard desktop environment (VirtualBox): https://yadi.sk/d/CLf4D4IM3TSpsx

  2. Debian 9 Minimal (.vmx) https://yadi.sk/d/BehMKGzS3LgXzG

Mirror: http://rubber-duck-typing.com/files/low-level-programming-debian-minimal.zip

  1. Debian 9 with XFCE installed (more user friendly, than minimal one if you are not a Linux expert; more lightweight than Ubuntu) https://yadi.sk/d/01uFWHCw3SHzh9.

Mirror: http://rubber-duck-typing.com/files/low-level-programming-debian-desktop.zip

For all virtual machines:

Username: user, password: user, root password: qwerty

  1. Docker image (thanks go to @kellyi ) https://github.com/kellyi/nasm-gcc-container

Releases

Release v1.0 corresponds to the code in the published book, without corrections or updates.

Contributions

See the file Contributing.md for more information on how you can contribute to this repository.

I want to personally thank Kunio Yoshikawa for his amazing and most effective contributions in finding errors and typos in the book.

I also thank all readers who have provided me with feedback, critique and corrections. Your work benefits the community!

low-level-programming's People

Contributors

abhinav-upadhyay avatar dskkato avatar gwens avatar kneoyskw avatar ngoclinhng avatar sayon avatar yeah-boi avatar yorovsobir avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

low-level-programming's Issues

Question 22

Sorry, I don't know the best way to contact you. I'm curious if you have some sources you can direct me to for question 22? I understand the previous two question with tail call optimization, but am having trouble with understanding how to implement co-routines without a jmp. Thank you.

Docker setup

That's awesome that preconfigured Linux virtual machines are available. Is there any interest in supporting Docker? I could put together a Dockerfile, get the image into Docker Hub, and provide some directions if it would be useful.

Q39

How do we multiply rax by 9 using exactly one instruction?

Would this be acceptable as an alternative?

IMUL rax, 9

typo in errata

Page 291, Section 15.1
the errata says
(i.e., dynamic loader or dynamic loader)
Do you mean "or dynamic linker"?

SYMBOL TABLE in Listing 5-28 not correct

Hi, i think the symbol table is not correct. it has UND and it includes no symbol from symbols.o. I tried the same link process and got the result:

SYMBOL TABLE:
00000000004000b0 l    d  .text	0000000000000000 .text
00000000006000fc l    d  .data	0000000000000000 .data
000000000060011c l    d  .bss	0000000000000000 .bss
0000000000000000 l    df *ABS*	0000000000000000 symbols.asm
00000000006000fc l       .data	0000000000000000 datavar1
0000000000600104 l       .data	0000000000000000 datavar2
000000000060011c l       .bss	0000000000000000 bssvar1
000000000260011c l       .bss	0000000000000000 bssvar2
00000000004000db l       .text	0000000000000000 textlabel
0000000000000000 l    df *ABS*	0000000000000000 executable_object.asm
0000000000600114 l       .data	0000000000000000 private
000000000060010c g       .data	0000000000000000 somewhere
00000000004000d8 g       .text	0000000000000000 _start
000000000060011c g       .bss	0000000000000000 __bss_start
00000000004000f0 g       .text	0000000000000000 func
000000000060011c g       .data	0000000000000000 _edata
0000000002600128 g       .bss	0000000000000000 _end

Q34

Possible typo, but I may have mistaken something

_no:
; REPLACE  in this branch, we are sure that 13 <= rax <= 42
; WITH     in this branch, we are sure that rax < 13 or 42 < rax
; ...

Nasm can't create output file

Hello, I'm running Debian 10 on a virtual machine and I installed docker and used the nasm-gcc-container that you recommend in this link https://github.com/kellyi/nasm-gcc-container , however, I can get to the part where there's "llp@fa82417446cc:/usr/src#" on the terminal screen put when I try compile and execute a program. I'm sorry if you have nothing to do with the docker, but I want to solve this problem in order to keep studying, I've already searched on internet, but no answer for this problem on Linux.

image

Edit: I couldn't download the pre-configurated virtual machine because the download speed is horrible in my region and the mirror is off.

Cheers

Q40

label:
neg rax
jl label
; here rax holds its absolute value

jl responds to SF != OF. js seems to work as well and is a "logical" choice.

Is jl preferred for any reason?

There is no need in having `pyc` files present in the repository

These files are meant to be created and used by the user interpreter on user machine. It is quite big chance that these files will be deleted by python interpreter itself because of incompatibility (different python interpreters may produce different byte code). If you look at python repositories, you will never see pyc files commited.

As of termcolor.pyc, I suggest to provide with source code instead.

Listing 5-31. libso corrupt?

something wrong with 2 command lines in Listing 5-31.
After two nasm lines, the book says:

ld -o main.o -d libso.so
ld -shared -o libso.so libso.o --dynamic-linker=/lib64/ld-linux-x86-64.so2

which does not work because libso.so is not yet generated.
Refering to the Makefile in libso directory and making it all right,
i guessed that the 2 lines shown above should be replaced with:

ld -shared -o libso.so libso.o
ld -o main main.o -d libso.so --dynamic-linker=/lib64/ld-linux-x86-64.so.2

login on minimal.vmdk

I purchased Kindle version. Installed the .vmdk on Virtual Box. The Debian prompts for a login. I haven't been able to find this login information. How can I learn the login?

Python scripts compatibility

  1. Python scripts should choose correct interpreter.

    #!/usr/bin/python2 or #!/usr/bin/python3 or better #!/usr/bin/env python2 etc.

  2. There should be compatible or both versions for python 2 and python 3. I can't run the script with python3 and I don't need and I don't have python 2 installed on my system. To be more user(reader)-friendly, I suggest to provide with compatible scripts (which can run in both 2 and 3 versions of Python), or different versions of the same script for 2 and 3 versions accordingly.

11.7.2 Assignment #6. "the modules" not clear

On page 218 of the book, in the section
"11.7 Assignment: Higher-Order Functions and Lists"
It says "6. Implement map_mut; using it,
output the modules of the input numbers"
I am wondering what "the modules" mean.

VirtualBox display resolution

Hello,

I am new to Linux, Debian and VirtualBox. Is there anyway to increase the screen resolution beyond 1024*768? Yes, there are forum threads out there that are not easy to understand, but I'd like to spend time on Low Level Programming instead and would appreciate simple instructions. Thank you!

display

Question / Answers: Combined

I do most of my reading on the subway to work. It generally has very poor Internet connectivity. The book has a number of questions / exercises, but the answers are only available here. I've been printing out multiple answers to bring along with me, but this is becoming a hassle as each answer is on a separate page. 289 separate pages, to be precise. It would be much more convenient if there was a combined page with all of the questions and answers. At the very least, this such a combined question / answer page should be available on a per-chapter basis.

Stackpointer subtraction in listings 14-4, 14-5, 14-6

The assembly code for listing 14-4 in listing 14-5 seems a bit wierd. RSP is not subtracted enough (only 0xf90) to hold char buffer[1024]. buffer is partly outside the stack and both a and b are completly outside the stack.

...
4004ba:       48 81 ec 90 0f 00 00    sub    rsp,0xf90
4004c1:       89 bd fc ef ff ff       mov    DWORD PTR [rbp-0x1004],edi
4004c7:       89 b5 f8 ef ff ff       mov    DWORD PTR 
...

Same issue with 14-6.

When I compiled and disassembled myself I got some extra bloat at 40055d-40056a and 400588-400597 (maybe you know why?), but RSP is subtracted more than enough (x1020) to hold buffeer, a and b:

$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc main.c -o test

$objdump -d -M intel test

...
0000000000400546 <maximum>:
  400546:	55                   	push   rbp
  400547:	48 89 e5             	mov    rbp,rsp
  40054a:	48 81 ec 20 10 00 00 	sub    rsp,0x1020
  400551:	89 bd ec ef ff ff    	mov    DWORD PTR [rbp-0x1014],edi
  400557:	89 b5 e8 ef ff ff    	mov    DWORD PTR [rbp-0x1018],esi
  40055d:	64 48 8b 04 25 28 00 	mov    rax,QWORD PTR fs:0x28
  400564:	00 00 
  400566:	48 89 45 f8          	mov    QWORD PTR [rbp-0x8],rax
  40056a:	31 c0                	xor    eax,eax
  40056c:	8b 85 ec ef ff ff    	mov    eax,DWORD PTR [rbp-0x1014]
  400572:	3b 85 e8 ef ff ff    	cmp    eax,DWORD PTR [rbp-0x1018]
  400578:	7d 08                	jge    400582 <maximum+0x3c>
  40057a:	8b 85 e8 ef ff ff    	mov    eax,DWORD PTR [rbp-0x1018]
  400580:	eb 06                	jmp    400588 <maximum+0x42>
  400582:	8b 85 ec ef ff ff    	mov    eax,DWORD PTR [rbp-0x1014]
  400588:	48 8b 55 f8          	mov    rdx,QWORD PTR [rbp-0x8]
  40058c:	64 48 33 14 25 28 00 	xor    rdx,QWORD PTR fs:0x28
  400593:	00 00 
  400595:	74 05                	je     40059c <maximum+0x56>
  400597:	e8 84 fe ff ff       	call   400420 <__stack_chk_fail@plt>
  40059c:	c9                   	leave  
  40059d:	c3                   	ret    
...

Section 1.3.1

Hi,

The book says 'The writes into 32-bit parts, however, fill the upper 32 bits of the full register with sign bits. For example, zeroing eax will zero the entire rax, storing -1 into eax will fill the upper 32 bits with ones.'.

However Intel manual says in section 3.4.1.1:

32-bit operands generate a 32-bit result, zero-extended to a 64-bit result in the destination general-purpose register

I checked this in Visual Studio using a simple program:

.code
SomeFunction proc
	mov eax, -1
	ret
SomeFunction endp
end

The RAX register was zero extended as described in Intel manual.

Is this an error in the book?

Thanks and Regards
Dibyendu

Assignment 2.7 string_copy definition doesn't match test harness or solution code

The definition for string_copy:

Accepts a pointer to a string, a pointer to a buffer, and buffer's length. Copies string to the destination. The destination address is returned if the string fits the buffer; otherwise zero is returns.

This is similar to strncpy in C. But the provided solution is the equivalent of strcpy. And because the caller in the test harness doesn't provide the buffer length in rdx an implementation that implements the behaviour described in the book will not pass the test.

Question 44 typo

I believe that there's a small typo in question 44 at the end of chapter 2. The value in rax should be "0x1122334455667788", not "0x112233445567788".

Q70

I would like to respectfully suggest an answer for question 70. But there are always better ways, even if this may work, and I'd be grateful for such thoughts.

%assign limit 15			; limit can be no higher than 63 with this code
%assign primestring 0		; bit-string holding our primes 1=prime; 0=composite
%assign n 3
%rep limit
	%assign primeflag 1		; 1 = n is prime; 0 = n is composite		
	%assign i 1
	%rep n/2
		%assign i i+1
		%if n % i = 0
			%assign primeflag 0
			%exitrep
		%endif
	%endrep
	%assign primestring primestring | (primeflag << n)
	%assign n n+1
%endrep
	dq primestring
		
%define isprime(n) (primestring >> n) & 1
; test the function with some outputs
	db 15, isprime(15) ; expect 0
	db 17, isprime(17) ; expect 1

produces

$ nasm -E primes.asm
%line 17+1 primes.asm
 dq 141480

 db 15, (141480 >> 15) & 1 ; expect 0
 db 17, (141480 >> 17) & 1 ; expect 1

something went wrong with Listing 16-2. stack_unwind.asm

I built an executable file with the following Makefile in listings/chap16/stack_unwind. When executed, the program printed 14 lines (not 10) and caused Segmentation fault. I think something is wrong with the loop termination logic in stack_unwind.asm. I don't know how rbx register value become zero. Any suggestion?

all: main

main: stack_unwind.o main.o 
	gcc -o main main.o stack_unwind.o

stack_unwind.o: stack_unwind.asm
	nasm -felf64 stack_unwind.asm -o stack_unwind.o

main.o: stack_unwind.c
	gcc -c -O0 stack_unwind.c -o main.o

Virtual Machine

There are no README files in the virtual machine downloads to guide set up. The book said there would be. Any help would be greatly appreciated. Thanks

Listing 15-10

I'm confused about the use of wrt ..plt in Listing 15-10.

Why state that sofun should use a PLT entry in the assembly code? Isn't it the static linkers job to determine what symbols should be bound dynamically (and therefore use GOT/PLT)?

If wrt ..plt is omitted, the same code is generated (in particular GOT/PLT is still used for sofun).

Confused by text at page 307

I'm confused by the following text at page 307:

When the variable is declared as global with its size and type (:data), it will live in the .data section of the executable file rather than the library! Because of this, you will always have to access it through GOT, even in the same file.

Is this really true? Lets modify ex2-main.asm like this:

extern _GLOBAL_OFFSET_TABLE_

global _start

extern sofun

global msg:data (msg.end - msg)

section .rodata

    msg: db "SO function called -- message is stored in 'main'", 10
    .end:

section .text
    
    _start:
        call sofun  wrt ..plt

        mov rax, 1
        mov rdi, 1
        mov rsi, msg ; No need to access via GOT.
        mov rdx, 50
        syscall

        mov rdi, 0
        mov rax, 60
        syscall

Here we are accessing msg directly without GOT and things seem to work fine (the string is printed twice).

error when run on Mac

I tried to run hello.asm. I can compile & build on my Mac but get error message when run it. Error: Bus error: 10

The executable file produced in the libso listing of chapter 5 does not work.

The executable file produced in the libso listing of chapter 5 does not work. The problem is reproduced below:

$ nasm -f elf64 -o main.o main.asm
$ nasm -f elf64 -o libso.o libso.asm
$ ld -shared -o libso.so libso.o --dynamic-linker=/lib64/ld-linux-x86-64.so.2
$ ld -o main main.o -d libso.so

Executing main does not work:

$ LD_LIBRARY_PATH=. ./main
bash: ./main: No such file or directory

The problem seems to be with the last command. If we change it to using gcc instead of ld, the execution works:

gcc -nostdlib -o main main.o libso.so

$ LD_LIBRARY_PATH=. ./main
Shared object 

It should be possible to fix this without using gcc instead of ld, but I don't know how.

VM user

Upon booting VM must provide username/password for valid user. I would expect to find this information in the README.

SSH into VM not working

I had an issue connecting to the VM. In the end networking turned out to be misconfigured.
In /etc/network/interfaces I replaced ens33 with enp0s3 which is what my $sudo ip a has shown.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.