4

A program typically issues a software trap when the program requires servicing by the operating system. The general exception handler for the operating system determines the reason for the trap and responds appropriately.

Is the assembly instruction trap alike the instruction TRAP in BASIC? The answer seems to be yes. Can you accept or reject my conclusion?

Code for "no interruption" is according to my instructions:

noint:
    PUSH r8
    movia r8,0x003b683a # machine code for TRAP
    ldw et,-4(ea) # read instr closest to return
    cmpeq et,et,r8 # compare
    POP r8
    bne et,r0,TrapHandler # if equal, jump to traphandler

Update

AFAIK in BASIC you can write e.g.

10 TRAP 20

to make row 20 the row for handling an error.

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • 2
    Which BASIC dialict are you referring to? – Jens Björnhager Nov 26 '12 at 01:37
  • @JensBjörnhager Commodore 64. I found trap in BASIC here: http://www.classic-games.com/commodore64/cbmtoken.html – Niklas Rosencrantz Nov 26 '12 at 01:43
  • broken link. [this](http://www.ict.kth.se/courses/IS1500/2012/kurslitt/n2sw_nii5v2.pdf) is what you were linking to, right? – keyser May 25 '14 at 12:45
  • @keyser Yes. I suppose that gcc can't generate all assembly instructions (e.g. `TRAP`). – Niklas Rosencrantz May 26 '14 at 04:11
  • Well it generates software traps all the time, for system calls. I don't know the specific instruction though, maybe `INT`. – keyser May 26 '14 at 09:58
  • GCC won't emit x86 `int 0x80` or x86-64 `syscall` itself; that part of libc uses inline asm or hand-written wrapper functions for system calls. As far as the compiler is concerned, `ssize_t write(int fd, void *buf, size_t len)` is just like any other function prototype in a header, no different from `sprintf`. (Modulo GCC knowing about some specific ISO C function names so it can optimize them more.) – Peter Cordes Jul 10 '22 at 07:58

2 Answers2

6

Not sure what TRAP does in BASIC, but the TRAP instruction in the assembler manual that you linked generates a hardware exception that can be handled by the operating system.

There is rarely need for a programmer to use this instruction in their code. The typical use of it is to be inserted by a debugger into the code being debugged at the point where a stop is desired (breakpoint), then running/continuing the program, and regaining control once the TRAP instruction is reached.

Alexey Feldgendler
  • 1,792
  • 9
  • 17
4

User processes can transition into the operating system kernel voluntarily, to request that the kernel do some operation on the user’s behalf. A system call is any procedure provided by the kernel that can be called from user-level. Most processors implement system calls using a special trap instruction.

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319