CS456 - Systems Programming


Exercise #3

Convert the ascii.c file to X86_64 assembly code (in ascii.s). To do this I recommend copying the ascii.c file as ascii.s and begin converting the code line by line. You may also wish to do this in iterative fashion, such as in the following steps:

  1. It is not recommended that your program use the following registers for anything other than making system calls: rax, rdi, rsi, rdx, r10, r8 or r9 (i.e. the registers used for system calls,) nor r11 (which seems to be used by the kernel as well.) Consider only using rbx or r12-r15.

  2. Make the function to print a character (printchar). Place a label called printchar at the beginning of the function, then make the write system call. Assume the character value to be printed is in the bl register, store that value in some memory location that has been set aside for the character data using the MOV instruction. The write system call number (1) should be placed in rax, the descriptor number (1 = stdout) should be placed in the rdi register, the address of the memory holding the character value should be placed in rsi and the length (1) should be placed in rdx. After the registers have been setup, make the SYSCALL, then RET (return) from the CALL'ed function.

          ; store bl in memory
          ; setup registers for the write syscall
          ; make the syscall
          ; return from function.

    ; elsewhere in the program (say _start or printascii, etc):
    ; set bl with the character to be printed
    ; CALL printnum
    ; do more stuff...
  1. Test the printchar function by printing the ASCII values from 32 to 126.

  2. Make the printnum function. Test it by first printing one number, then test it by printing all the numbers from 0 to 127.

  3. Make the printascii function and test it with a specific ASCII value.

  4. Complete the _start routine with code equivalent to what is in main() in the C program.